爱玺玺

爱玺玺的生活日记本。wx:lb87626

android 自定义控件添加修改属性的方法

package cn.jianhaozhan.ui;


import cn.jianhaozhan.activity.R;

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

import android.widget.CheckBox;

import android.widget.RelativeLayout;

import android.widget.TextView;


public class SettingView extends RelativeLayout {

private TextView title_tv;

private TextView des_tv;

private CheckBox cb;

//在布局文件中使用时调用,多了个样式

public SettingView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}

    //在布局文件中使用时调用

public SettingView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

    //在代码中使用时调用

public SettingView(Context context) {

super(context);

init();

}

    //自己增加的样式

private void init(){

//第一种方式

// View view = View.inflate(getContext(), R.layout.settingview, null);

// this.addView(view);

//第二种方式

View view = View.inflate(getContext(), R.layout.settingview, this);

title_tv = (TextView) view.findViewById(R.id.title);

des_tv = (TextView) view.findViewById(R.id.des);

cb = (CheckBox) view.findViewById(R.id.cb);

}

/**

* 设置标题

* @param title

*/

public void setTitle(String title){

title_tv.setText(title);

}

/**

* 设置内容

* @param des

*/

public void setDes(String des){

des_tv.setText(des);

}

/**

* 设置选中状态

* @param isChecked

*/

public void setCheckBox(boolean isChecked){

cb.setChecked(isChecked);

}

/**

* 获取checkbox状态

* @return

*/

public boolean isChecked(){

return cb.isChecked();

}

}


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5