android:XML切换按钮但不切换功能

时间:2014-02-23 14:44:52

标签: android xml genymotion

我得到一个奇怪的行为,我不确定我应该这样做。

我有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>

我想切换按钮,以便“next”和“prev”位于真假的下方。 当我移动第一个线性布局块时,在第二个之后。我按正确的顺序获得按钮但是prev和next按钮表现为True / False按钮,而True / False按钮表现为下一个/上一个(功能保持相同的顺序)

我做错了什么? (我是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;
    }

}

2 个答案:

答案 0 :(得分: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中重新排列。我不知道你在说什么功能。

您需要在Activity中引用正确的ID。我猜你也在转换id。

编辑:

看完编辑的帖子后我没有看到任何错误。我建议您清理并构建项目或尝试卸载并重新安装。

答案 1 :(得分:0)

我删除了R.java文件,它重新创建了另一个文件,现在它可以工作了。不知道为什么。

非常感谢。