按钮setOnClickListener无法正常工作

时间:2015-11-25 10:29:07

标签: android android-layout android-fragments

我是Android新手,一整天都为这个问题疯狂......

我有自定义的preferenceFragment

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);

            View v = inflater.inflate(R.layout.parent_login, null);


            Button b = (Button)v.findViewById(R.id.parentLoginButton);

            if (b != null) {

                Log.i("Settings", "I find the button");

                b.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        Log.i("Settings", "he he");
                        
                    }
                });
            } else {
                Log.i("Settings", "I can't find the button");
            }

            return view;
        }

实际运行的结果是按钮'b'不为null,可以找到,但是setOnClickListener似乎不起作用,当我点击按钮时,显示以下日志:

11-25 18:28:16.484 562-601/? I/InputReader: Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.376 ] when=19678293069000
11-25 18:28:16.484 562-600/? I/InputDispatcher: Delivering touch to: action: 0x4, toolType: 1
11-25 18:28:16.484 562-600/? I/InputDispatcher: Delivering touch to: action: 0x0, toolType: 1

...

请帮帮我..

src:

下的xml文件

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
        android:title="@string/pref_header_account"
        android:key="pref_parent_login"
        android:layout="@layout/parent_login" />

</PreferenceScreen>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <EditText
        android:id="@+id/parentLoginEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/parent_account_email"
        android:textSize="16dp" />

    <EditText
        android:id="@+id/parentLoginPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/parent_account_password"
        android:textSize="16dp"
        android:inputType="textPassword" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log In"
        android:id="@+id/parentLoginButton"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/textview"
        android:layout_toStartOf="@+id/textview" />

</LinearLayout>

5 个答案:

答案 0 :(得分:0)

<强> public View RootView; // Global

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

         if (RootView == null) {


          RootView = inflater.inflate(R.layout.your_layout, container, false);

          Button button = (Button) RootView.findViewById(R.id.Your_Button_Id):
          button.setOnClickListener(new OnClickListener()
           {
             @Override
             public void onClick(View v)
             {
                // do something
             } 
     }); 

   return RootView;

答案 1 :(得分:0)

试试这个。我不确定,但尝试一次。

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


        View v = inflater.inflate(R.layout.parent_login, null);


        Button b = (Button)v.findViewById(R.id.parentLoginButton);

            b.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    Log.i("Settings", "he he");

                }
            });

        return v;
    }

答案 2 :(得分:0)

您必须返回v而不是视图。 你可以分开它:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                            Bundle savedInstanceState) {
        View  v = inflater.inflate(R.layout.content_main, null);
        b = (Button)v.findViewById(R.id.parentLoginButton);
        return v;
    }

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (b != null) {
            Log.i("Settings", "I find the button");
            b.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                Log.i("Settings", "he he");
            }
            });
        } else {
            Log.i("Settings", "I can't find the button");
        }
    }   

答案 3 :(得分:0)

主线程可能被循环使用。因此主线程无法处理与UI相关的其他事件。

答案 4 :(得分:0)

您可以删除行

View view = super.onCreateView(inflater, container, savedInstanceState);

并返回v,而不是视图

相关问题