Android LinearLayout 设置 OnClickListener 无效时解决方法

被这个问题缠了一下午。


1. 自定义控件是 xml 布局文件中根元素(或父元素)使用了 android:clickable 属性,在设置 View.OnClickLinstener 时无法响应onClick 事件:



2.删除 android:clickable 属性,如果需要使用这个属性,在代码中设置:

this.setClickable(true);

3.再次监听 onClick 事件,正常。


其实不止是 LinearLayout,像在 RelativeLayout中指定 android:clickable 属性,同样会使 onClick 事件无法被监听。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于您没有提供具体的注册界面效果,我将提供一个基本的注册界面实现方法,您可以根据自己的需求进行修改和优化。 首先,在XML布局文件中创建一个注册界面的布局。可以使用LinearLayout或RelativeLayout等布局容器来设计您的界面。 例如,以下是一个基本的注册界面布局: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <TextView android:id="@+id/textView_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/username" android:textColor="@color/black" android:textSize="16sp" android:layout_marginTop="20dp" android:layout_marginLeft="20dp"/> <EditText android:id="@+id/editText_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/username_hint" android:textColorHint="@color/grey" android:textColor="@color/black" android:inputType="text" android:layout_below="@id/textView_username" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> <TextView android:id="@+id/textView_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/password" android:textColor="@color/black" android:textSize="16sp" android:layout_below="@id/editText_username" android:layout_marginTop="20dp" android:layout_marginLeft="20dp"/> <EditText android:id="@+id/editText_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password_hint" android:textColorHint="@color/grey" android:textColor="@color/black" android:inputType="textPassword" android:layout_below="@id/textView_password" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> <Button android:id="@+id/button_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/register" android:background="@drawable/button_background" android:textColor="@color/white" android:textSize="16sp" android:layout_below="@id/editText_password" android:layout_marginTop="30dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> </RelativeLayout> ``` 接下来,您需要在Java代码中实现注册功能。在您的Activity或Fragment中,创建一个方法来处理用户的注册请求。该方法应该包括以下步骤: 1. 获取用户名和密码EditText的值。 2. 验证用户名和密码是否为空或无效。 3. 发送注册请求到服务器。 4. 处理服务器响应并显示适当的消息。 以下是一个简单的注册方法示例: ``` private void registerUser() { String username = editText_username.getText().toString().trim(); String password = editText_password.getText().toString().trim(); if(username.isEmpty()) { editText_username.setError("Please enter username"); editText_username.requestFocus(); return; } if(password.isEmpty()) { editText_password.setError("Please enter password"); editText_password.requestFocus(); return; } // 发送注册请求到服务器 // ... // 处理服务器响应并显示适当的消息 // ... } ``` 最后,您需要在注册按钮的点击事件中调用该方法。例如,您可以在onCreate方法中为注册按钮添加一个点击事件监听器: ``` Button buttonRegister = findViewById(R.id.button_register); buttonRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { registerUser(); } }); ``` 以上就是一个基本的注册界面实现方法。您可以根据自己的需求进行自定义和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值