单击另一个按钮后如何防止用户点击按钮?

时间:2018-06-11 07:32:19

标签: android

我是Android编程的新手,正在建立一个测验应用程序,其中一个问题有4个选项,如果用户点击其中一个选项,其他选项应该是不可点击的。我目前只能制作一个不可点击的按钮。这是java代码。

package com.example.android.quiz;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}




//This method is called when option 1 of question 1 is selected
public void verifyQuestion1Option1(View view) {
    Button QuestionOption1 = (Button) findViewById(R.id.question1_option1);
    QuestionOption1.setBackgroundColor(getResources().getColor(R.color.solid_red));

    question1Answer();

}

public void verifyQuestion1Option2(View view) {
    Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
   Question1Option2.setBackgroundColor(getResources().getColor(R.color.solid_red));//solid red is not a predefined colour. It is declared in colors.xml

    question1Answer();

}

public void verifyQuestion1Option3(View view) {
    Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
    Question1Option3.setBackgroundColor(getResources().getColor(R.color.solid_green));

    question1Answer();

}

public void verifyQuestion1Option4(View view) {
    Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
    Question1Option4.setBackgroundColor(getResources().getColor(R.color.solid_red));//We call the getResources() method because R.colour.solid_red passed the id of the color not the actual colour value.

    question1Answer();

}

public void question1Answer() {
    TextView q1Answer = (TextView) findViewById(R.id.question1_answer);
    String answer = "Rajinish Kumar is the current Chairman of SBI who took over after Arundhati Bhattacharya retired on 6 October.Shikha Sharma is the Managing Director and CEO of Axis Bank and Chanda Kochhar is the managing director and CEO of ICICI Bank";
    q1Answer.setText(answer);
}

}

2 个答案:

答案 0 :(得分:2)

您可以使用在任何时间点只有1个活动按钮的buttongroup,否则,您需要以编程方式禁用其他按钮。

要禁用该按钮,您可以使用以下代码:

Button button = (Button) findViewById(R.id.button);
button.setEnabled(false);

答案 1 :(得分:0)

您可以使用.setEnabled(false);禁用按钮。该按钮将变灰并且不再响应点击事件。

要禁用所有按钮,请获取每个按钮的句柄并将其设置为禁用。

Button Question1Option1 = (Button) findViewById(R.id.question1_option1);    
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);    
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option1.setEnabled(false);
Question1Option2.setEnabled(false);
Question1Option3.setEnabled(false);
Question1Option4.setEnabled(false);

这样,此问题的所有按钮都会被禁用。 您还可以提供一个解决方案,保存按钮已被按下并忽略进一步的单击事件。您可以引入某种变量bool question1answered = false;,只要触发onClick事件就将其设置为true。

public void verifyQuestion1Option4(View view) {
  if (question1Answered == true) {return;}
  question1Answered =true;
  //Do the rest of your checks here

  }

编程Java的两个技巧:

  1. Java(与C#相反)使用低位字母变量作为约定。 Button question1Option1 = (Button) findViewById(R.id.question1_option1);将是更好的方式。

  2. 如果您有更多问题,将它们放入某种阵列并多次重复使用相同的四个按钮是有意义的。如果你必须改变一些东西,这将节省你很多编程开销和代码重写。它使代码更清晰。