java按钮视图即使在将其可见性设置为true后也不可见
我正在制作一个测验应用程序,因此我希望我的MenuButton
和NextButton
视图在开始时不可见,但在满足特定条件后,我希望视图再次可见,因此我使用了findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
和NextButton
视图,但这些视图仍然不可见。我做错了什么
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_gravity="center">
<TextView
安卓:id="@+id/Question"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="46dp"
安卓:text="TextView"
安卓:gravity="center"
安卓:textSize="25dp"
安卓:textColor="#000000"/>
<RadioButton
安卓:id="@+id/radioButton1"
安卓:layout_width="wrap_content"
安卓:layout_height="25dp"
安卓:text="RadioButton1"
安卓:textSize="20sp"
安卓:textColor="#000000"
安卓:layout_marginLeft="43dp"
安卓:layout_marginStart="43dp"
安卓:layout_marginTop="54dp"
安卓:layout_below="@+id/Question"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true"
安卓:onClick="On_RadioButton1_Click"/>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/linearLayout"
安卓:orientation="horizontal"
安卓:layout_marginTop="55dp"
安卓:layout_below="@+id/radioButton4"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true">
<Button
安卓:id="@+id/MenuButton"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:layout_marginLeft="40dp"
安卓:textSize="20dp"
安卓:text="menu" />
<Button
安卓:id="@+id/NextButton"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textSize="20dp"
安卓:layout_marginLeft="125dp"
安卓:text="next" />
</LinearLayout>
</RelativeLayout>
//Java文件
private int Question_no=0;
private Boolean Boolean_Var=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
String[] Question_Array = getResources().getStringArray(R.array.Question1);
TextView Questions = (TextView) findViewById(R.id.Question);
Questions.setText(Question_Array[Question_no]);
String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
Radio_Button1.setText(Radio_Button1_Array[Question_no]);
findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);
}
public void On_RadioButton1_Click(View view)
{
if(Boolean_Var==false)
{
String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
String CorrectAns = CorrectAns_Array[Question_no];
String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
String Answer = Answer_Array[Question_no];
if(Answer.equals(CorrectAns))
{
RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
Right_Ans.setTextColor(Color.GREEN);
AnswerSubmitted();
}
else
{
RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
Wrong_Ans.setTextColor(Color.RED);
GreenTick();
AnswerSubmitted();
}
}
Boolean_Var=true;
}
public void AnswerSubmitted()
{
findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}
# 1 楼答案
您应该获得按钮的引用,并在引用上调用setVisibility。您当前正在做的是在一个引用上设置可见性
INVISIBLE
,在另一个引用上设置可见性VISIBLE
# 2 楼答案
我认为你应该告诉android,当你的radiobutton被点击时,你想执行这个方法。加入:
所以你每次点击都可以执行这个方法。 将其添加到OnCreate的末尾