我通过制作一个带有3个问题的琐事应用来学习构建Android应用:1使用单选按钮,1个自由文本字段,1个带复选框。
逻辑是,一旦用户选择或输入答案,单选按钮,复选框和EditText将显示为灰色,然后用户将点击分数按钮以获得带有分数和自定义消息的Toast。 / p>
我有两个问题:
(1)EditText没有灰显 - 我尝试使用setFocusable()
和setEnabled()
进行多种变体无效。
(2)即使输入的答案是正确的,分数变量也不会将问题2(带有EditText的那个)分配给1分。
我从头开始,但我还没有能够解决这些问题。任何线索?
我的XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:padding="16dp"
android:background="#fefcf1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/seinfeld_trivia_logo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Who are the 4 main characters?"
android:textColor="#000"
android:textSize="16sp"
android:layout_marginBottom="5dp"/>
<RadioGroup
android:id="@+id/question_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/question_one_answer_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jerry, Elaine, George, Kessler"
android:textSize="14sp"
android:onClick="onRadioButtonQ1Clicked"/>
<RadioButton
android:id="@+id/question_one_answer_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jerry, Elaine, George, Kramer"
android:textSize="14sp"
android:onClick="onRadioButtonQ1Clicked"/>
<RadioButton
android:id="@+id/question_one_answer_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Newman, Jerry, Kramer, George"
android:textSize="14sp"
android:onClick="onRadioButtonQ1Clicked"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Kramer's given name?"
android:textColor="#000"
android:textSize="16sp"
android:layout_marginBottom="5dp"/>
<EditText
android:id="@+id/question_two_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onQ2AnswerEntered"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is correct about Festivus? Check all that apply."
android:textColor="#000"
android:textSize="16sp"
android:layout_marginBottom="5dp"/>
<CheckBox
android:id="@+id/question_three_answer_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There is an aluminum pole"
android:layout_marginBottom="5dp"
android:onClick="onCheckBoxesQ3Clicked"/>
<CheckBox
android:id="@+id/question_three_answer_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You eat ham"
android:layout_marginBottom="5dp"
android:onClick="onCheckBoxesQ3Clicked"/>
<CheckBox
android:id="@+id/question_three_answer_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You air your grievances"
android:layout_marginBottom="5dp"
android:onClick="onCheckBoxesQ3Clicked"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/reset_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:textColor="#fff"
android:text="Try Again"
android:layout_gravity="center_horizontal"
android:layout_marginRight="30dp"
android:onClick="resetQuiz"/>
<Button
android:id="@+id/score_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:textColor="#fff"
android:text="Show me my score"
android:layout_gravity="center_horizontal"
android:padding="20dp"
android:onClick="showScoreMessage"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
我的Java
package com.example.android.seinfeldtrivia;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
//Declaring variables - Question 1
RadioButton Q1A1, Q1A2, Q1A3;
RadioGroup Q1RadioGroup;
//Declaring variables - Question 2
EditText Q2AnswerEditText;
//Declaring variable for extracting the string from the EditText on Q2
String question2Answer;
//Declaring variables - Question 3
CheckBox Q3A1, Q3A2, Q3A3;
//Declaring variables to check the state of the checkboxes on Q3
boolean checkedQ3A1, checkedQ3A2, checkedQ3A3;
//Declaring variable - Score
int score;
//Declaring variable for the score and reset button
Button scoreMe, resetQuiz;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing views - Question 1
Q1RadioGroup = (RadioGroup) findViewById(R.id.question_1);
Q1A1 = (RadioButton) findViewById(R.id.question_one_answer_one);
Q1A2 = (RadioButton) findViewById(R.id.question_one_answer_two);
Q1A3 = (RadioButton) findViewById(R.id.question_one_answer_three);
//Initializing views - Question 2
Q2AnswerEditText = (EditText) findViewById(R.id.question_two_answer);
//Initializing views - Question 3
Q3A1 = (CheckBox) findViewById(R.id.question_three_answer_one);
Q3A2 = (CheckBox) findViewById(R.id.question_three_answer_two);
Q3A3 = (CheckBox) findViewById(R.id.question_three_answer_three);
//Initializing views - Score & Reset buttons
scoreMe = (Button) findViewById(R.id.score_me);
resetQuiz = (Button) findViewById(R.id.reset_me);
}
//To disable the radio groups once the user has chosen an answer
public static void enableQuestion(View view, boolean enabled) {
view.setEnabled(enabled);
view.setFocusable(enabled);
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) view;
for (int i = 0; i < vg.getChildCount(); i++)
enableQuestion(vg.getChildAt(i), enabled);
}
}
//Getting the values of the answers entered
/*
* Checking which answer was selected - Question 1
*/
public void onRadioButtonQ1Clicked(View view) {
// Check that the user chose an answer
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.question_one_answer_one:
if (checked) {
yourAnswerQ1 = Q1A1;
enableQuestion(Q1RadioGroup, false);
break;
}
//Correct answer
case R.id.question_one_answer_two:
if (checked) {
yourAnswerQ1 = Q1A2;
score++;
enableQuestion(Q1RadioGroup, false);
break;
}
case R.id.question_one_answer_three:
if (checked) {
yourAnswerQ1 = Q1A3;
enableQuestion(Q1RadioGroup, false);
break;
}
}
}
/*
* Checking which answer was selected - Question 2
*/
public void onQ2AnswerEntered(View view) {
question2Answer = Q2AnswerEditText.getText().toString();
question2Answer = question2Answer.toLowerCase();
if (question2Answer.equals("cosmo")) {
//If the question is not empty, check if the answer is correct and add one point to the score
//and display the score msg
score++;
}
Q2AnswerEditText.setEnabled(false);
}
/*
* Checking which answers were selected - Question 3
*/
public void onCheckBoxesQ3Clicked(View view) {
checkedQ3A1 = Q3A1.isChecked();
checkedQ3A2 = Q3A2.isChecked();
checkedQ3A3 = Q3A3.isChecked();
if (checkedQ3A1 && checkedQ3A3) {
score++;
}
// Check which checkbox button was checked
switch(view.getId()) {
case R.id.question_four_answer_one:
if (checkedQ3A1) {
yourAnswerQ3A1 = Q3A1;
enableQuestion(Q3A1, false);
break;
}
case R.id.question_four_answer_two:
if (checkedQ3A2) {
yourAnswerQ3A2 = Q3A2;
enableQuestion(Q3A2, false);
break;
}
case R.id.question_four_answer_three:
if (checkedQ3A3) {
yourAnswerQ3A3 = Q3A3;
enableQuestion(Q3A3, false);
break;
}
}
}
public void showScoreMessage(View view) {
String scoreMessage = String.format(getResources().getString(R.string.your_score_is), score);
String customMessage = "";
//To display a short message together with the score, according to what score the user got
if (score == 0 || score == 1) {
customMessage = "No soup for you!";
} else if (score == 2) {
customMessage = "Do a fit of strength and try again.";
} else {
customMessage = "You're a master of your domain.";
}
//Displaying the score message and the score
Toast.makeText(this, scoreMessage + customMessage, Toast.LENGTH_SHORT).show();
}
public void resetQuiz(View view) {
//Enabling the questions
enableQuestion(Q1RadioGroup, true);
//Resetting questions: clearing the checked radio buttons and checkboxes
Q1RadioGroup.clearCheck();
//Resetting questions: Q3 - deleting the text in the EditText
Q3AnswerEditText.setText("");
Q3AnswerEditText.setEnabled(true);
//if the checkboxes are checked, then uncheck and enable them
if (checkedQ3A1) {
Q3A1.setChecked(false);
enableQuestion(Q3A1, true);
}
if (checkedQ3A2) {
Q3A2.setChecked(false);
enableQuestion(Q3A2, true);
}
if (checkedQ3A3) {
Q3A3.setChecked(false);
enableQuestion(Q3A3, true);
}
//Resetting the score variable
score = 0;
}
}
答案 0 :(得分:0)
使用android:enabled = false或editText.setEnabled(false) 它会使Edittext变灰。