Android include标签的监听事件处理

来源:互联网 发布:搬瓦工vps搭建ss优化 编辑:程序博客网 时间:2024/06/09 06:04
首先,应该知道include标签本身是不能设置监听的,不论是android:onClick 或者 是对 include 绑定监听器setOnClickListener,都是不会。不论一个界面有多少个include,android系统只是对include标签里面的子标签监听,但由于子标签只有一个,所以无法区分监听结果,其实解决办法也很简单,只需要单独对include标签的每个子标签重新设置 id 即可。下面是代码:

需要复写的xml文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="0dp"                android:layout_height="0dp">    <TextView        android:id="@+id/tv_setting_update_app_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="15dp"        android:layout_marginTop="10dp"        android:text="标题"        android:textColor="#f000"        android:textSize="18sp"/>    <TextView        android:id="@+id/tv_setting_des_msg"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/tv_setting_update_app_title"        android:layout_marginBottom="5dp"        android:layout_marginLeft="15dp"        android:text="描述信息"        android:textSize="12sp"/>    <CheckBox        android:id="@+id/ck_setting_update_app"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_marginRight="15dp"        android:clickable="false"        android:focusable="false"        android:background="#0000"/>    <TextView        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_below="@id/tv_setting_des_msg"        android:background="#5000"/></RelativeLayout>

布局文件:


<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.joker.activity.SettingActivity">    <TextView        android:id="@+id/tv_setting_page_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:background="#86f0"        android:gravity="center"        android:paddingBottom="5dp"        android:paddingTop="5dp"        android:text="设置菜单"        android:textSize="22sp"/>    <include        android:id="@+id/setting_menu_update_item"        layout="@layout/setting_menu_item_layout"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <include        android:id="@+id/setting_menu_update1_item"        layout="@layout/setting_menu_item_layout"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>

代码(代码中使用xutils框架IOC注解):

package com.joker.activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.TextView;import android.widget.Toast;import org.xutils.view.annotation.ContentView;import org.xutils.view.annotation.Event;import org.xutils.view.annotation.ViewInject;@ContentView(R.layout.activity_setting)public class SettingActivity extends BaseActivity {    //标题    @ViewInject(R.id.tv_setting_update_app_title)    private TextView tv_setting_update_app_title;    //描述信息    @ViewInject(R.id.tv_setting_des_msg)    private TextView tv_setting_des_msg;    //选择框    @ViewInject(R.id.ck_setting_update_app)    private CheckBox ck_setting_update_app;    //SharedPreferences    private SharedPreferences configInfoSp;    //可以监听一组按钮    @Event(value = {R.id.setting_menu_update_item, R.id.setting_menu_update1_item})    private void settingMenuClick(View view) {        SharedPreferences.Editor editor = configInfoSp.edit();        switch (view.getId()) {            case R.id.setting_menu_update_item:                Toast.makeText(SettingActivity.this, "点击测试", Toast.LENGTH_SHORT).show();                if (ck_setting_update_app.isChecked()) {                    tv_setting_des_msg.setText("当前处于关闭软件自动更新状态");                    ck_setting_update_app.setChecked(false);                    editor.putBoolean("appAutoUpdate", false);                } else {                    ck_setting_update_app.setChecked(true);                    tv_setting_des_msg.setText("当前处于开启软件自动更新状态");                    editor.putBoolean("appAutoUpdate", true);                }                break;            case R.id.setting_menu_update1_item:                Toast.makeText(SettingActivity.this, "点击测试1", Toast.LENGTH_SHORT).show();                break;            default:                break;        }        editor.commit();    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //初始化        settingPageInit();    }    private void settingPageInit() {        tv_setting_update_app_title.setText("软件更新");        //获得SharedPreferences对象的编辑器        configInfoSp = getSharedPreferences("configInfo", MODE_PRIVATE);        //获取数据        if(configInfoSp.getBoolean("appAutoUpdate",true)){            ck_setting_update_app.setChecked(true);            tv_setting_des_msg.setText("当前处于开启软件自动更新状态");        }else{            ck_setting_update_app.setChecked(false);            tv_setting_des_msg.setText("当前处于关闭软件自动更新状态");        }    }}

效果:





1 0
原创粉丝点击