网页无法访问此网站,做电商排名网站,简单网站页面设计,建设网站 请示 报告为了能让代码能够更多的复用#xff0c;故使用组合控件。下面是我正在写的项目中用到的方法。 1、先写要组合的一些需要的控件#xff0c;将其封装到一个布局xml布局文件中。 ?xml version1.0 encodingutf-8?
RelativeLayout xmlns:an…为了能让代码能够更多的复用故使用组合控件。下面是我正在写的项目中用到的方法。 1、先写要组合的一些需要的控件将其封装到一个布局xml布局文件中。 ?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_height68dipandroid:idid/aaaTextViewandroid:idid/tv_update_titleandroid:layout_widthmatch_parentandroid:layout_heightwrap_content android:textSize20spandroid:layout_marginTop10dpandroid:layout_marginLeft10dpandroid:text是否升级 /TextViewandroid:layout_belowid/tv_update_titleandroid:idid/tv_update_contentandroid:textSize15spandroid:layout_widthmatch_parentandroid:layout_heightwrap_content android:layout_marginTop10dpandroid:layout_marginLeft10dpandroid:text停止更新 /CheckBoxandroid:checkedfalseandroid:idid/cb_isupdateandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_alignParentRighttrueandroid:layout_centerVerticaltrue //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 version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical TextViewandroid:idid/tv_maintitleandroid:layout_widthmatch_parentandroid:layout_height55dpandroid:background#8866ff00android:gravitycenterandroid:text设置中心android:textSize22sp /com.frank.mobilesafe.ui.SettingItemViewandroid:idid/siv_updateandroid:layout_widthmatch_parentandroid:layout_heightwrap_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