我得到一個奇怪的行爲,我不知道我應該。android:XML開關按鈕,但不是功能
我有4個按鈕。 2是「真」和「假」。和另外兩個是「上一頁」和「下一頁」:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="24dp"
android:id="@+id/question_text_view" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prev_question_button"
android:drawableLeft="@drawable/arrow_left"
android:drawablePadding="4dp" />
<Button android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_question_button"
android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="wrap_content"
android:id="@+id/true_button"
android:layout_height="wrap_content" android:text="@string/true_button" />
<Button android:layout_width="wrap_content"
android:id="@+id/false_button"
android:layout_height="wrap_content" android:text="@string/false_button" />
</LinearLayout>
</LinearLayout>
我想切換按鈕,這樣的「下一步」,「上一頁」,將是真正的假下方。 當我移動第一個線性佈局塊後,第二個。我按正確的順序得到按鈕,但是上一個和下一個按鈕的行爲與True/False按鈕相同,True/False按鈕的行爲與Next/Prev(功能保持不變)
我做錯了什麼? (進出口新的XML和Android)
編輯: 我不切換的ID。所以我不能說出爲什麼這樣表現。
public class QuizActivity extends Activity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPrevButton;
private TextView mQuestionTextView;
private TrueFalse mQuestionBank[] = new TrueFalse[] {
new TrueFalse(R.string.question_aftrica, true),
new TrueFalse(R.string.question_americas, false),
new TrueFalse(R.string.question_asia, true),
new TrueFalse(R.string.question_oceans, false) };
private int mCurrentIndex = 0;
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean useRressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
System.out.println("check");
int textID;
if (useRressedTrue == answerIsTrue) {
textID = R.string.correct_toast;
} else {
textID = R.string.incorrect_toast;
}
Toast.makeText(this, textID, Toast.LENGTH_SHORT).show();
// System.out.println("check dis");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
mQuestionTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
checkAnswer(false);
}
});
mPrevButton = (Button) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
updateQuestion();
}
});
mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
updateQuestion();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
}
使用RelativeLAyout。你可以把你的按鈕相對於另一個 – Raghunandan
你是什麼意思的「開關按鈕,但不是功能」? – Raghunandan
我的意思是,當我按下「下一步」按鈕,我得到的是「真實」按鈕 – Yevgeni