项目里要求点击button后要加背景框,要变字体颜色,还要在前面加上小对勾,那么多按钮,总不能一个个的设置,只能自定义view了,因为项目中要用到checkbox,所以我就用以CheckBox为例了。
我一开始自定义CheckBox,报错:
java.lang.RuntimeException:Unable to start activity {com.example.administrator.csdntext/com.example.administrator.csdntext.MainActivity}:android.view.InflateException
找了半天原因,后来我发现是我构造方法没写对,直接上构造方法的代码:
public class ScreenCheckBox extends CheckBox {
public ScreenCheckBox(Context context) {
super(context);
setOnCheckedChangeListener();
initStyle();
}
//这个方法一定要写 否则会报错
public ScreenCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
setOnCheckedChangeListener();
initStyle();
}
public ScreenCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnCheckedChangeListener();
initStyle();
}
//这个方法是检测按钮初始化时是否是选中状态,如果是选中状态就要进行相应设置
public void initStyle(){
setBackgroundResource(R.drawable.radiobutton_style);
if (this.isChecked()){
setText("√"+getText());
setTextColor(Color.parseColor("#d52c34"));
}
}
//这个方法监控按钮选中状态的变化,并进行相应操作
public void setOnCheckedChangeListener() {
this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.setText("√" + getText());
setTextColor(Color.parseColor("#d52c34"));
} else {
buttonView.setText(getText().subSequence(1, getText().length()));
setTextColor(Color.parseColor("#000000"));
}
}
});
}
}
就是第二个构造方法,每次创建view时调用的都是这个构造方法。
自定义View写好了,就写布局文件吧:这里我就写了三个按钮而已
<com.example.administrator.csdntext.ScreenCheckBox
android:id="@+id/button1"
android:layout_below="@id/installTime"
android:layout_width="100dp"
android:layout_height="50dp"
android:button="@null"
android:gravity="center"
android:layout_margin="10dp"
android:text="aaaaaa"/>
<com.example.administrator.csdntext.ScreenCheckBox
android:id="@+id/button2"
android:layout_below="@id/button1"
android:layout_width="100dp"
android:layout_height="50dp"
android:button="@null"
android:checked="true"
android:gravity="center"
android:layout_margin="10dp"
android:text="bbbbbb"/>
<com.example.administrator.csdntext.ScreenCheckBox
android:id="@+id/button3"
android:layout_below="@id/button2"
android:layout_width="100dp"
android:layout_height="50dp"
android:button="@null"
android:gravity="center"
android:layout_margin="10dp"
android:text="cccccc"/>
在MainActivity直接调用就好了,太简单了:
public class MainActivity extends AppCompatActivity {
private ScreenCheckBox button1,button2,button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1= (ScreenCheckBox) findViewById(R.id.button1);
button2= (ScreenCheckBox) findViewById(R.id.button2);
button3= (ScreenCheckBox) findViewById(R.id.button3);
button3.setChecked(true);
}
}
代码剩下的就是背景文件了,也都很简单,我也贴上来:
这是radiobutton_style.xml,是背景文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/red_corner_border" android:state_checked="true"/>
<item android:drawable="@drawable/gray_corner_border" android:state_checked="false"/>
</selector>
这是选中状态下的red_corner_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="3dp"/>
<stroke
android:width="1dp"
android:color="#d52c34"/>
</shape>
这是未选中状态下的gray_corner_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="3dp"/>
<stroke
android:width="1dp"
android:color="#bfbfbf"/>
</shape>
就这么简单的东东,用了我半天时间,慢慢学慢慢积累吧。