Android:将按钮移动到不同的LinearLayout会导致点击事件混乱



我从事专业开发已经超过14年了,但我正在学习Android开发。我遇到了一个让我难以置信的场景。我设计了一个简单的布局,然后决定在同一布局文件中将一些按钮从一个LinearLayout移动到另一个Linear layout。

现在,由于移动了按钮,点击事件被连接到了错误的按钮!就好像按钮的资源ID是顺序相关的。

之前(工作正常):

<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按钮下方。而且,如果你看看设计师,它是完美无瑕的——完美!但是,控制器打嗝:

  • True按钮现在变成Previous按钮
  • False按钮现在变成True按钮
  • 上一个按钮现在变成False按钮
  • "下一步"按钮正常工作

这是连接点击事件的代码(很抱歉,如果代码看起来很蹩脚,我正在读一本安卓教程,所以请不要批评它,它不是我的!):

@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必须是特定于订单的;我无法用其他方式解释。问题是我不知道如何让我的按钮在我想要的布局中做他们想要的事情。

好吧,我想我设法解决了它。在Eclipse/ADT中,我去了Project->Clean。。。这似乎奏效了。是的,这是资源ID的问题!我试着在之前/之后发布一个git存储库的漂亮屏幕截图,这样你就可以看到R.java文件中的哪些行发生了变化,但我没有足够的信誉点。很抱歉

最新更新