zoukankan      html  css  js  c++  java
  • Android开发学习笔记-自定义组合控件

    为了能让代码能够更多的复用,故使用组合控件。下面是我正在写的项目中用到的方法。

    1、先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="68dip"
        android:id="@+id/aaa"
        >
            <TextView
            android:id="@+id/tv_update_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:text="是否升级" />
            <TextView
            android:layout_below="@id/tv_update_title"
            android:id="@+id/tv_update_content"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:text="停止更新" />
    
            <CheckBox
                android:checked="false"
                android:id="@+id/cb_isupdate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true" />
    
        </RelativeLayout>

    2、自定义Java类

    package com.frank.mobilesafe.ui;
    
    import com.frank.mobilesafe.R;
    
    import android.R.bool;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    public class SettingItemView extends RelativeLayout {
        private CheckBox cb_update;
        private TextView tv_update_title;
        private TextView tv_update_content;
    
        public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initView(context);
        }
    
        private void initView(Context context) {
            // TODO Auto-generated method stub
            View.inflate(context, R.layout.setting_item_view, this);
            cb_update = (CheckBox) findViewById(R.id.cb_isupdate);
            tv_update_title =  (TextView) findViewById(R.id.tv_update_title);
            tv_update_content = (TextView) findViewById(R.id.tv_update_content);
    
        }
    
        public SettingItemView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initView(context);
        }
    
        public SettingItemView(Context context) {
            super(context);
            initView(context);
        }
    
        /**
         * 检查是否选中
         * @return
         */
        public boolean isChecked() {
            return cb_update.isChecked();
        }
        /**
         * 设置组合控件的状态
         * @param isChecked
         */
        public void SetChecked(boolean isChecked) {
            cb_update.setChecked(isChecked);
        }
        /**
         * 设置描述信息
         * @param isChecked
         */
        public void SetDesc(String text) {
            tv_update_content.setText(text);
        }
    }

    3、在主界面中引用

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_maintitle"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#8866ff00"
            android:gravity="center"
            android:text="设置中心"
            android:textSize="22sp" />
    
       <com.frank.mobilesafe.ui.SettingItemView
           android:id="@+id/siv_update"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           />
    </LinearLayout>

    4、主界面调用

    public class SettingActivity extends Activity {
    
        private SettingItemView siv_update;
        private SharedPreferences sp_update;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_setting);
            siv_update = (SettingItemView) findViewById(R.id.siv_update);
            sp_update = getSharedPreferences("config",MODE_PRIVATE);
            boolean  update = sp_update.getBoolean("update", false);
            if (update) {
                siv_update.SetChecked(true);
                siv_update.SetDesc("有新版本则更新");
            }
            else
            {
                siv_update.SetChecked(false);
                siv_update.SetDesc("停止更新");
            }
            siv_update.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Editor editor = sp_update.edit();
                    // TODO Auto-generated method stub
                    if (siv_update.isChecked()) {
                        siv_update.SetChecked(false);
                        siv_update.SetDesc("停止更新");
                        editor.putBoolean("update", false);
                    }
                    else{
                        siv_update.SetChecked(true);
                        siv_update.SetDesc("有新版本则更新");
                        editor.putBoolean("update", true);
                    }
                }
            });
        }
    
        
    }

    5、完成

    正常显示

  • 相关阅读:
    hihoCoder #1062 : 最近公共祖先·一
    hihoCoder #1050 : 树中的最长路
    hihoCoder #1049 : 后序遍历
    108 Convert Sorted Array to Binary Search Tree 将有序数组转换为二叉搜索树
    107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II
    106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
    105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
    104 Maximum Depth of Binary Tree 二叉树的最大深度
    102 Binary Tree Level Order Traversal 二叉树的层次遍历
    101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/4009881.html
Copyright © 2011-2022 走看看