为什么setOnClickListener不适用于膨胀按钮?

时间:2016-01-27 19:30:02

标签: android button onclicklistener android-inflate

我是初学者,我正在从tab_fragment.xml膨胀按钮,当我尝试将setOnClickListener设置为该按钮时,它无法正常工作

LayoutInflater inflater=getLayoutInflater();
    View v=inflater.inflate(R.layout.tab_fragment, (ViewGroup) findViewById(R.id.line1));
    Button button= (Button) v.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            viewPagerAdapter.addFrag(new Tab(), "two");
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }
    });
这是我的 tab_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/line1"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
    <TextView
    android:layout_margin="50dp"
    android:text="some text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal"
    />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

确定。得到它了。该按钮是片段布局的一部分。因此,您没有活动类中的按钮实例。如果要处理活动类上的按钮单击行为,可以使活动实现OnClickListener。然后从片段覆盖onAttach()方法,如下所示:

@Override
public void onAttach(Context activity) {
    super.onAttach(activity);
    if(activity instanceof OnClickListener)
        mListener = (OnClickListener) activity;
    }
}

@Override
public void onDetach() {
    mListener = null;
    super.onDetach();
}

然后在onCreateView方法上:

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

此外,从onClick()中的onClick(from fragment)移动逻辑,如下所示:

Override
public void onClick(View view) {
    switch (view.getId() == R.id.button) {
          // move your logic here
    }
}

答案 1 :(得分:0)

你应该在片段类中有这样的东西:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_fragment, container, false);
    Button button = (Button) v.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            viewPagerAdapter.addFrag(new Tab(), "two");
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return v;
}