android按钮监听器无法正常工作

时间:2015-09-03 17:54:33

标签: android button android-fragments layout

我有一个包含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>

2 个答案:

答案 0 :(得分:0)

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() {

}

答案 1 :(得分:0)

如果这是您的完整代码。您的退货声明在onCreateView方法中的哪个位置。

下面,

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的对象层次结构)