android="
xmlns:
app="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
tools:
context="firstandroidapp_by150104050020.myapplication.MainActivity">
xmlns:
android="
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_gravity="center">
android:
id="@+id/button"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
gravity="center"
android:
text="改变当前背景颜色"
android:
textSize="25dp"/>
MainActivity.java
packagefirstandroidapp_by150104050020.myapplication;
importandroid.graphics.Color;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassMainActivityextendsAppCompatActivity{
privateButtonbutton;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)this.findViewById(R.id.button);
finalint[]flag={0};
button.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewv){
switch(flag[0]){
case0:
button.setBackgroundColor(Color.RED);
Toast.makeText(MainActivity.this,"当前背景色为红色",Toast.LENGTH_LONG).show();
flag[0]=1;
break;
case1:
button.setBackgroundColor(Color.GREEN);
Toast.makeText(MainActivity.this,"当前背景色为绿色",Toast.LENGTH_LONG).show();
flag[0]=2;
break;
case2:
button.setBackgroundColor(Color.BLUE);
Toast.makeText(MainActivity.this,"当前背景色为蓝色",Toast.LENGTH_LONG).show();
flag[0]=3;
break;
case3:
button.setBackgroundColor(Color.YELLOW);
Toast.makeText(MainActivity.this,"当前背景色为黄色",Toast.LENGTH_LONG).show();
flag[0]=0;
break;
}
}
});
}
}
运行效果:
4.
程序代码:
activity_main.xml
xmlversion="1.0"encoding="utf-8"?
>
android="
xmlns:
app="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
tools:
context="firstandroidapp_by150104050020.myapplication.MainActivity">
xmlns:
android="
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_gravity="center">
id="@+id/imagebutton"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
src="@drawable/button"
android:
onClick="showdialog0"/>
id="@+id/checkbox1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="Chinese"/>
id="@+id/checkbox2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="English"/>
id="@+id/button1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="复选按钮确定"
android:
onClick="showdialog1"/>
id="@+id/radiogroup"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
orientation="horizontal">
id="@+id/radiobutton1"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="male"/>
id="@+id/radiobutton2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="female"/>
id="@+id/button2"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="单选按钮确定"
android:
onClick="showdialog2"/>
MainActivity.java
packagefirstandroidapp_by150104050020.myapplication;
importandroid.content.DialogInterface;
importandroid.support.v7.app.AlertDialog;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.CheckBox;
importandroid.widget.RadioButton;
publicclassMainActivityextendsAppCompatActivity{
privateCheckBoxcheckbox1;
privateCheckBoxcheckbox2;
privateRadioButtonradiobutton1;
privateRadioButtonradiobutton2;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox1=(CheckBox)this.