package safeluck.drive.evaluation.fragment.rulefragments.cview; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.TextView; import safeluck.drive.evaluation.R; /** * DriveJudge * Created by lzw on 2020/9/24. 10:25:00 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class TextCheckBox extends LinearLayout implements View.OnClickListener { private CheckBox checkBox; private boolean isChecked = false; private static final String TAG = "TextCheckBox"; public TextCheckBox(Context context) { this(context,null); } public TextCheckBox(Context context, AttributeSet attrs) { this(context, attrs,0); } public TextCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr,0); } public TextCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initView(context,attrs); } private void initView(Context context, AttributeSet attrs) { View view = LayoutInflater.from(context).inflate(R.layout.layout_text_checkbox,this,true); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextCheckBox_attr_tvcheck); String string = typedArray.getString(R.styleable.TextCheckBox_attr_tvcheck_txt_des_check); boolean aBoolean = typedArray.getBoolean(R.styleable.TextCheckBox_attr_tvcheck_check_not, false); isChecked = aBoolean; TextView text = view.findViewById(R.id.tv_checkbox); text.setText(string); checkBox = view.findViewById(R.id.check_setting); checkBox.setChecked(aBoolean); checkBox.setClickable(false); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean Checked) { Log.i(TAG,"点击了checkbox checked="+Checked); isChecked = Checked; } }); setOnClickListener(this); } @Override public void onClick(View v) { Log.i(TAG,"点击,未取反之前isChecked="+isChecked); isChecked =!isChecked; checkBox.setChecked(isChecked); } /** * 获取Checkbox是否选中状态,选中为true * * @return */ public boolean isChecked(){ return isChecked; } }