片段实现View.OnClickListener - 按钮不起作用

时间:2015-08-06 10:32:08

标签: android button android-fragments view

为什么我的按钮没有显示toast消息并写入日志。按钮位于片段内?

我的MainActivity.java中的代码

 public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

我的FragmentA.java中的代码:

    public class FragmentA extends Fragment implements   View.OnClickListener{

    Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.button);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getActivity(), "Do it", Toast.LENGTH_LONG).show();
        Log.w("myApp","Hi there");
    }
}

fragment_a.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFBB00"
    >


    <Button
        android:layout_margin="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        />

</LinearLayout>

activity_main.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BBFF"
    >


    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.audiorecbas.audio_recorder_basic.FragmentA"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

4 个答案:

答案 0 :(得分:2)

  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.button);
        button.setOnClickListener(this);
    }

答案 1 :(得分:2)

在Fragment Java类中,您需要具备以下功能来处理Button的点击事件:就像这样

    public class FragmentA extends Fragment implements   View.OnClickListener
    {
           Button btn;

         @Override
         public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);

                btn = (Button) getActivity().findViewById(R.id.button);
                btn.setOnClickListener(this);       

            ......    
            }

有关更多信息,请查看SO ANSWER

答案 2 :(得分:0)

在实例化后,您还没有关联按钮的onClickListener。

button = (Button) getActivity.findViewById(R.id.button);
button.setOnClickListener(this); //add this

答案 3 :(得分:0)

删除整个代码

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    button = (Button)getActivity().findViewById(R.id.button);

}

然后添加

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);
    button= (Button) view.findViewById(R.id.button);
    button.setOnClickListener(this);
    return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            {
             //Do your stuff here
            }
         }
      }