日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发学习笔记-自定义组合控件

發布時間:2023/12/20 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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开发学习笔记-自定义组合控件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。