我已经完成了14年的专业发展,但我正在学习Android开发。我遇到了令我难以置信的场景。我设计了一个简单的布局,然后决定将一些按钮从一个LinearLayout移动到同一个布局文件中的另一个LinearLayout。
现在,由于移动按钮,点击事件会连线到错误的按钮!就像按钮的资源ID依赖于顺序一样。
BEFORE(正常工作):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous_button" />
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"/>
</LinearLayout>
AFTER(工作不正常):
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous_button" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"/>
</LinearLayout>
正如您在“AFTER”代码中看到的,我希望上一个/下一个按钮位于True / False按钮下方。而且,如果你看看设计师,它是完美的 - 完美!但是,控制器打嗝:
这是连接点击事件的代码(对不起,如果代码看起来很蹩脚,我正在阅读一本Android教程书,所以请不要批评它,它不是我的!):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int length = mQuestionBank.length;
mCurrentIndex = (mCurrentIndex + (length-1)) % length;
updateQuestion();
}
});
updateQuestion();
}
所以这些资源ID必须是特定于订单的;我无法用其他任何方式解释它。问题是我不知道如何让我的按钮在我想要的布局中做他们想要的。
答案 0 :(得分:0)
好的,我想我设法解决了这个问题。在Eclipse / ADT中,我去了Project - &gt;干净......而这似乎可以解决问题。是的,这是资源ID的问题!我尝试在之前/之后发布一个很好的git存储库屏幕截图,这样你就可以看到R.java文件中的哪些行发生了变化,但是我没有足够的声誉点。遗憾。