Android Fragment onClick按钮方法

use*_*725 85 android view click button

我正在尝试在我的onClick(View v)XML中调用该方法,但不能与Fragment一起使用.这是错误.

01-17 12:38:36.840: E/AndroidRuntime(4171): java.lang.IllegalStateException: 
Could not find a method insertIntoDb(View) in the activity class main.MainActivity 
for onClick handler on view class android.widget.Button with id 'btn_conferma'
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public void insertIntoDb(View v) {
...
} 
Run Code Online (Sandbox Code Playgroud)

xml代码

<Button
        android:id="@id/btn_conferma"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0.0dip"
        android:layout_height="45dp"
        android:layout_marginLeft="2dp"
        android:layout_weight="1.0"
        android:background="@drawable/bottoni"
        android:gravity="center_horizontal|center_vertical"
        android:onClick="insertIntoDb"
        android:text="SALVA"
        android:textColor="#ffffff"
        android:textSize="16sp" />
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 175

你的活动必须有

public void insertIntoDb(View v) {
...
} 
Run Code Online (Sandbox Code Playgroud)

不是碎片.

如果您不想在活动中进行上述操作.初始化片段中的按钮并将侦听器设置为相同.

<Button
    android:id="@+id/btn_conferma" // + missing
Run Code Online (Sandbox Code Playgroud)

然后

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

   View view = inflater.inflate(R.layout.fragment_rssitem_detail,
    container, false);
   Button button = (Button) view.findViewById(R.id.btn_conferma);
   button.setOnClickListener(new OnClickListener()
   {
             @Override
             public void onClick(View v)
             {
                // do something
             } 
   }); 
   return view;
}
Run Code Online (Sandbox Code Playgroud)

  • 我对上面的评论感到有些惊讶.没有冒犯,但只是在没有发布任何细节的情况下说"它不起作用"是非常不专业的......至于我,"它只是有效". (3认同)

cja*_*m13 13

另一种选择可能是让您的片段实现View.OnClickListener并覆盖片段中的onClick(View v).如果您需要让片段与活动通信,只需添加具有所需方法的接口,并让活动实现接口并覆盖其方法.

public class FragName extends Fragment implements View.OnClickListener { 
    public FragmentCommunicator fComm;
    public ImageButton res1, res2;
    int c;

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            fComm = (FragmentCommunicator) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement FragmentCommunicator");
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        res1 = (ImageButton) getActivity().findViewById(R.id.responseButton1);
        res1.setOnClickListener(this);
        res2 = (ImageButton) getActivity().findViewById(R.id.responseButton2);
        res2.setOnClickListener(this);
    }
    public void onClick(final View v) { //check for what button is pressed
            switch (v.getId()) {
                case R.id.responseButton1:
                  c *= fComm.fragmentContactActivity(2);
                  break;
                case R.id.responseButton2:
                  c *= fComm.fragmentContactActivity(4);
                  break;
                default:
                  c *= fComm.fragmentContactActivity(100);
                  break;
    }
    public interface FragmentCommunicator{
        public int fragmentContactActivity(int b);
    }



public class MainActivity extends FragmentActivity implements FragName.FragmentCommunicator{
    int a = 10;
    //variable a is update by fragment. ex. use to change textview or whatever else you'd like.

    public int fragmentContactActivity(int b) {
        //update info on activity here
        a += b;
        return a;
    }
}
Run Code Online (Sandbox Code Playgroud)

http://developer.android.com/training/basics/firstapp/starting-activity.html http://developer.android.com/training/basics/fragments/communicating.html

  • 我一直这样做,但仍然觉得它很难看 (3认同)

Cza*_*att 6

这不是问题,这是Android的设计.看到这里:

您应该将每个片段设计为模块化和可重用的活动组件.也就是说,因为每个片段使用自己的生命周期回调定义自己的布局和自己的行为,所以可以在多个活动中包含一个片段,因此您应该设计为重用并避免直接操作另一个片段中的一个片段.

可能的解决方法是在MainActivity中执行以下操作:

Fragment someFragment;    

...onCreate etc instantiating your fragments

public void myClickMethod(View v){
    someFragment.myClickMethod(v);
}
Run Code Online (Sandbox Code Playgroud)

然后在你的Fragment类中:

public void myClickMethod(View v){
    switch(v.getid()){
       // Your code here
    }
 } 
Run Code Online (Sandbox Code Playgroud)


Jay*_*gra 5

对于 Kotlin 用户:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?, 
    savedInstanceState: Bundle?) : View?
{
    // Inflate the layout for this fragment
    var myView = inflater.inflate(R.layout.fragment_home, container, false)
    var btn_test = myView.btn_test as Button
    btn_test.setOnClickListener {
        textView.text = "hunny home fragment"
    }

    return myView
}
Run Code Online (Sandbox Code Playgroud)