There is a correct answer, and there are 4 VIEWS (probably these are buttons?), When pressed, the text from the button is compared with the correct answer and the next task goes, but how then to make the correct answer not on one button, but on the other a button? So that the user doesn’t always press the right button and get the right answer? How to implement this correctly? Verifying the selected answer with the correct answer occurs with the help of equals
Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants Yuriy SPb ♦ , pavel , Ivan Pshenitsyn , aleksandr barakin , dirkgntly 21 Aug '16 at 17:24 .
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Depending on the code, how the check is done is not clear, for example, you can compare the inscription on the button with the correct answers via equals (if you added the code how the check happens, there would be a specific answer and so many ways) - Artem
- @ Artem corrected. Simply, is it not a crutch 4 buttons and so checked? - Martinez Toni
- Well, if checking through equals what's the problem, change the buttons in places? Kostylno for whom? 1 line of code (create arrays with answers and correct answers shove them into the loop and check) change the question and answers through the arrays - Artem
- @ Artem, how can the buttons change places? Can you give an example of the code that you wrote? - Martinez Toni
- Well, just the text, change the position of the same, fret right now, an example of a roll - Artem
|
1 answer
I have not tried to write like that before. but I would write this Example:
package com.example.amon.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { String[] question = {"5-2", "5+3", "7-2", "7-2", "7-2"}; String[] mAnswer = {"3","37","7", "7","3","8", "5","3","3","3","3","3", "5", "3","3","3","5"}; String[] answer = {"3", "8", "5", "5", "5"}; TextView quest,Answerq; Button one,two,three; int pos = 0; int prav_otvet = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); quest= (TextView) findViewById(R.id.textView2); Answerq= (TextView) findViewById(R.id.textView4); one = (Button) findViewById(R.id.button); two = (Button) findViewById(R.id.button2); three = (Button) findViewById(R.id.button3); one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); quest.setText(question[0]); one.setText(mAnswer[0]); two.setText(mAnswer[1]); three.setText(mAnswer[2]); } @Override public void onClick(View view) { pos = pos+1; quest.setText(question[pos]); one.setText(mAnswer[pos+4]); two.setText(mAnswer[pos+3]); three.setText(mAnswer[pos+1]); Toast toast = Toast.makeText(getApplicationContext(), "ответ Неправильный ", Toast.LENGTH_SHORT); toast.show(); if (((Button) view).getText().toString().equals(answer[pos])){ prav_otvet = prav_otvet + 1; Answerq.setText(String.valueOf(prav_otvet)); Toast toast1 = Toast.makeText(getApplicationContext(), "правильно ", Toast.LENGTH_SHORT); toast.show(); } } } and markup:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.amon.myapplication.MainActivity" tools:showIn="@layout/activity_main"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="46dp" android:layout_marginStart="46dp" android:layout_marginBottom="154dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" android:layout_alignBottom="@+id/button" android:layout_toRightOf="@+id/button" android:layout_toEndOf="@+id/button" android:layout_marginLeft="32dp" android:layout_marginStart="32dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Сколько будет ?" android:id="@+id/textView" android:layout_marginTop="32dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView2" android:layout_marginTop="83dp" android:layout_below="@+id/textView" android:layout_alignLeft="@+id/button3" android:layout_alignStart="@+id/button3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Праильных ответов:" android:id="@+id/textView3" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/textView4" android:layout_alignTop="@+id/textView3" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> - In the mAnswer line, change the numbers in places as desired by 3 buttons count to one of them was the correct answer - Artem
|
