2014-02-23 41 views
0

我得到一個奇怪的行爲,我不知道我應該。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; 
    } 

} 
+0

使用RelativeLAyout。你可以把你的按鈕相對於另一個 – Raghunandan

+0

你是什麼意思的「開關按鈕,但不是功能」? – Raghunandan

+0

我的意思是,當我按下「下一步」按鈕,我得到的是「真實」按鈕 – Yevgeni

回答

1

使用RelativeLayout是更好的,雖然它可以與LinearLayout

http://developer.android.com/guide/topics/ui/declaring-layout.html

I want to switch the buttons so the "next" and "prev" will be underneath the true false 

的意見可以被重新安排在XML來實現。我不知道你在說什麼功能。

您需要在活動中引用正確的ID。我想你也是切換ID。

編輯:

看後期編輯我看不出有什麼錯後。我建議你清理並建立你的項目或嘗試卸載並重新安裝。

+0

**編輯** 代碼如果活動,如你所見。沒有改變與ID的。 – Yevgeni

+1

@YevgeniTarler沒有問題。我不明白你在說什麼功能。或嘗試卸載並重新安裝 – Raghunandan

0

我刪除了R.java文件,它重新創建了另一個,現在它工作。不知道爲什麼。

無論如何。

+0

如果您清理並構建您的項目也應該工作,因爲它會重新生成 – Raghunandan

+0

它會在每次清理後自動生成。有沒有更好的辦法? – Yevgeni

+0

通常這不應該發生。假設你添加一個帶有id的新資源,立即在R.java中創建一個條目。所以它的更新。在你的情況下,它din't。清理和構建應該修復它。 – Raghunandan