今天完成内容:
学习andriod checkbox
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="clip_horizontal">
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:textColor="@color/chocolate"
android:checked="true"/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色" />
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色" />
</LinearLayout>
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/show"/>
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="clip_horizontal">
<CheckBox
android:id="@+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:textColor="@color/chocolate"
android:checked="true"/>
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色" />
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色" />
</LinearLayout>
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/show"/>
</TableLayout>
<color name="chocolate">#D2691E</color><!--巧可力色 →
<color name="black">#000000</color><!--黑色 -->
<color name="chocolate">#D2691E</color><!--巧可力色 →
<color name="black">#000000</color><!--黑色 -->
public class MainActivity extends Activity{
private TextView show;
private CheckBox box1,box2,box3;
private String love= "我喜欢色是";
private String not= "不是我喜欢的";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
show = (TextView) findViewById(R.id.show);
box1 = (CheckBox) findViewById(R.id.box1);
box2 = (CheckBox) findViewById(R.id.box2);
box3 = (CheckBox) findViewById(R.id.box3);
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(show.getVisibility()==View.VISIBLE){
show.setText(love+box1.getText());
textColor();
}else {
show.setVisibility(View.VISIBLE);
show.setText(love+box1.getText());
textColor();
}
}else{
show.setText(not);
setTextBlack();
}
}
});
//box2 and box3 和box1的写法一样,在这就不重复写了;
private void textColor() {
box3.setTextColor(getApplicationContext().getColor(R.color.chocolate));
}
private void setTextBlack() {
box3.setTextColor(getApplicationContext().getColor(R.color.black));
}
}
}
看视频
遇到问题:
无
明日目标:
学习Android studio的开发