【问题标题】:Android: Moving Button to different LinearLayout messes up Click EventsAndroid:将按钮移动到不同的 LinearLayout 会弄乱点击事件
【发布时间】:2013-12-30 05:23:36
【问题描述】:

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

现在,由于移动了按钮,点击事件被连接到了错误的按钮!就好像按钮的资源 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>

之后(工作不正确):

<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 按钮现在变为上一个按钮
  • False 按钮​​现在变为 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 必须是特定于订单的;我无法以任何其他方式解释它。问题是我不知道如何让我的按钮在我想要的布局中做他们想做的事情。

【问题讨论】:

    标签: android-layout button view onclick android-linearlayout


    【解决方案1】:

    好的,我想我设法解决了它。在 Eclipse/ADT 中,我转到 Project -> Clean...,这似乎可以解决问题。是的,这是资源 ID 的问题!我尝试在之前/之后发布 git 存储库的漂亮屏幕截图,以便您可以看到 R.java 文件中的哪些行发生了更改,但我没有足够的声誉点。对不起。

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多