Android开发学习笔记-自定义组合控件
生活随笔
收集整理的這篇文章主要介紹了
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"><TextViewandroid: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="是否升級" /><TextViewandroid: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="停止更新" /><CheckBoxandroid: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 stubView.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" ><TextViewandroid: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.SettingItemViewandroid: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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {Editor editor = sp_update.edit();// TODO Auto-generated method stubif (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、完成
正常顯示
轉載于:https://www.cnblogs.com/xuhongfei/p/4009881.html
總結
以上是生活随笔為你收集整理的Android开发学习笔记-自定义组合控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CPC客户端离线升级失败,不能获取upd
- 下一篇: querydsl动态 sql_JPA整合