【问题标题】:android button listener not workingandroid按钮监听器不起作用
【发布时间】:2015-09-03 17:54:32
【问题描述】:

我有一个包含 layout_a 的片段。在该片段下,我动态添加了一个 textview 和一个按钮,它是 layout_b 的一部分。我无法让按钮(layout_b 的 myButton 部分)侦听器工作。我还有其他按钮,它们是 layout_a 的一部分并且正在工作。要在 layout_b 中使用按钮侦听器,我正在执行以下步骤。

public View onCreateView(LayoutInflater in, ViewGroup vgroup, Bundle savedInstanceState) {
        testView = inflater.inflate(R.layout.layout_b, container, false);
         ...
         myButton = (Button) testView.findViewById(R.id.my_button);
         ...
}
private void configureSuggestedEmailButton() {
        myButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.i("test", "click");
                ...

            }
        });
    }

片段布局是layout_a,按钮在layout_b,我需要做些不同的事情吗?

更新代码:

public View onCreateView(LayoutInflater in, ViewGroup vgroup, Bundle savedInstanceState) {
         //this view is used to setup buttons from layout_a, current activity has this layout
            view = inflater.inflate(R.layout.layout_a, container, false);
         //this view is used to manage to the layout_b
            testView = inflater.inflate(R.layout.layout_b, container, false);
            myButton = (Button) testView.findViewById(R.id.my_button);

            configureSuggestedEmailButton();
    }
private void configureSuggestedEmailButton() {
            myButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Log.i("test", "click");


                }
            });
   }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/layout_b"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <Button
            style="?android:attr/buttonStyleSmall"
            android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="30dip"
            android:text="Yes"
            android:textSize="10sp"

            />

    </RelativeLayout>

</LinearLayout>

【问题讨论】:

  • 所以您正在将这个新部分添加到 layout_a 中,对吧?在你的代码中你是在哪里做的?
  • @Diyoda 我有一个函数调用,它执行以下LinearLayout row = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.layout_b, null); LinearLayout eLayout = (LinearLayout) editTextField.getParent(); eLayout.addView(errorRow, someIndex); ... 按钮显示在屏幕上,但监听器没有响应。我的看法是按钮侦听器未附加到片段
  • 你能用所有相关代码更新问题吗,我一定会帮你的
  • @diyoda 我更新了代码,如果您需要更多信息,请告诉我。
  • 看看我的代码。可能存在语法问题,我没有使用我的 IDE 来写这个。我想传达这个想法。这是你已经这样做的方式吗?我希望你错过了你认为不相关的代码部分。

标签: android button android-fragments layout


【解决方案1】:

如果这是您的完整代码。 onCreateView 方法中的 return 语句在哪里。

这里,

public View onCreateView(LayoutInflater in, ViewGroup vgroup, Bundle savedInstanceState){
               //this view is used to setup buttons from layout_a, current activity has this layout
                view = inflater.inflate(R.layout.layout_a, container, false);

                // Here get the reference to the UI element which you want to inject the layout_b
                injectReferece = (LinearLayout)view.findViewById(R.id.injectElement);// In this case Assume that the the hoder of the layout_b is a `LinearLayout`, You have to specify that in your layout_a with the id injectElement(In this example ok :))



                //this view is used to manage to the layout_b
                testView = inflater.inflate(R.layout.layout_b, container, false);
                myButton = (Button) testView.findViewById(R.id.my_button);

                configureSuggestedEmailButton();

                // Then you actually add it, 
                injectReferece.addView(myButton );
                return view;
}

我看到你膨胀了 layout_a,你必须在 onCreateView 方法结束时返回它。但在此之前(返回view 变量)您再次膨胀了layout_b。

但是,您必须使用 addView() 方法将其添加到膨胀的 layout_a(即内存中 layout_a 的对象层次结构)中

【讨论】:

  • 这实际上是我的错误,我确实在 onCreateView 的末尾返回了 layout_a,但我没有将 secondlayout 添加到该视图中。我需要使用 ViewGroup 吗?或者有没有办法将第二个视图添加到第一个视图中。
  • 正如我所说,在您的 layout_a 中添加一个特定位置(injectElement 或其他任何内容)。并引用 (injectReferece) 然后执行 injectReferece.addView(myButton );。我在代码中的cmets中解释了。
  • 我给了你一个赞成票,但是一旦我得到 15 分,我就会变得可见(参考我收到的弹出消息)。再次感谢您的建议。 :)
【解决方案2】:
public View onCreateView(LayoutInflater in, ViewGroup vgroup, Bundle savedInstanceState) {
    testView = inflater.inflate(R.layout.layout_b, container, false);
     ...
     myButton = (Button) testView.findViewById(R.id.my_button);

     myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("test", "click");
            ...
        }
    });
     ...
}
 private void configureSuggestedEmailButton() {

}

【讨论】:

  • 你好,我正在调用onCreateView下的configureSuggestedEmailButton()方法。问题依然存在。感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多