我正在製作測驗應用程序,因此我想要我的MenuButton
和NextButton
視圖在開始時不可見但在滿足特定條件後我希望視圖再次可見,因此我使用了findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
和NextButton
一樣,但仍然這些視圖是隱形的。我做錯了什麼?即使設置其可見性後,按鈕視圖也不可見
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="@+id/Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="TextView"
android:gravity="center"
android:textSize="25dp"
android:textColor="#000000"/>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:text="RadioButton1"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="54dp"
android:layout_below="@+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="On_RadioButton1_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:orientation="horizontal"
android:layout_marginTop="55dp"
android:layout_below="@+id/radioButton4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="@+id/MenuButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:text="menu" />
<Button
android:id="@+id/NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="125dp"
android: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);
}
在您的代碼中,鏈接到textView問題的按鈕的位置。如果問題文本太長,按鈕可能不在屏幕上。 – SolderingIronMen
文字顏色變紅嗎? – Ogbe
在你的課堂內製作一個按鈕的全局對象。在onCreate中初始化它。並使用該對象隱藏/顯示 –