android - 按钮 setOnClickListener 不起作用

标签 android android-layout android-fragments

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

我有一个自定义的首选项Fragment

       @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'不为空,可以找到,但是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>

最佳答案

您必须返回 v 而不是 view。 您可以将其拆分:

@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");
        }
    }   

关于android - 按钮 setOnClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914068/

相关文章:

android - 即使我的应用程序关闭,广播接收器也可以工作

android - 如何在折线的中心绘制标记

使用recyclerview时Android fragment 滚动所有元素

android - Eclipse IDE - 并排的 Android 图形布局和 XML 布局

java - Recyclerview 不显示项目?

java - 删除布局中按/触摸时的深色高光

Android Studio Google Map V2 fragment 渲染

即使在 onCreateView 中初始化后,android fragment View 也会抛出空指针异常

android - 查看分页器仅显示 1 个 ListView

android - 将 Activity 设置为滑动选项卡中的内容?