(1)ImageButton
button1.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/button1_focused" />
<item android:state_pressed="true" android:drawable="@drawable/button1_pressed"/>
<item android:drawable="@drawable/button1_normal" />
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:tag="imageBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button1"
android:paddingTop="5dip"/>
</RelativeLayout>
(2)Menu
/drawable 添加menu 的 png图片,values/string.xml添加相应的字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, DemoActivity!</string>
<string name="app_name">Demo</string>
<string name="showimage1">IMAGE ONE</string>
<string name="showimage2">IMAGE TWO</string>
<string name="showwhite">USE WHITE</string>
<string name="showalert">SHOW ALERT</string>
<string name="showblack">USE BLACK</string>
<color name="background">#000000</color>
<color name="background2">#FFFFFF</color>
</resources>
/menu新增mainmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/buttonone"
android:icon="@drawable/image1icon"
android:title="@string/showimage1"/>
<item
android:id="@+id/buttontwo"
android:icon="@drawable/image2icon"
android:title="@string/showimage2" />
<item
android:id="@+id/buttonthree"
android:icon="@drawable/menu3icon"
android:title="@string/showwhite" />
<item
android:id="@+id/buttonfour"
android:icon="@drawable/menu4icon"
android:title="@string/showblack" />
<item
android:id="@+id/buttonfive"
android:icon="@drawable/menu5icon"
android:title="@string/showalert" />
</menu>
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.main_menu, null);
setContentView(rootView);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
this.getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
RelativeLayout rl=(RelativeLayout) rootView.findViewWithTag("rl");
ImageView iv=(ImageView)rl.findViewWithTag("img");
switch(item.getItemId()){
case R.id.buttonone:
iv.setImageResource(R.drawable.image1);
return true;
case R.id.buttontwo:
iv.setImageResource(R.drawable.image2);
return true;
case R.id.buttonthree:
iv.setBackgroundResource(R.color.background2);
return true;
case R.id.buttonfour:
iv.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Making the Menu Work:
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="rl">
<ImageView
android:tag="img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"/>
</RelativeLayout>
Adding Dialogs:
Four custom subclasses of the Dialog class are:AlertDialog、ProgressDialog、DatePickerDialog、TimePickerDialog。
You can have up to three buttons in an AlertDialog object:PositiveButton、NegativeButton、NeutralButton.
here is the all java code:
public class DemoActivity extends Activity {
/** Called when the activity is first created. */
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView=LayoutInflater.from(this).inflate(R.layout.main_menu, null);
setContentView(rootView);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
this.getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
RelativeLayout rl=(RelativeLayout) rootView.findViewWithTag("rl");
ImageView iv=(ImageView)rl.findViewWithTag("img");
switch(item.getItemId()){
case R.id.buttonone:
iv.setImageResource(R.drawable.image1);
return true;
case R.id.buttontwo:
iv.setImageResource(R.drawable.image2);
return true;
case R.id.buttonthree:
iv.setBackgroundResource(R.color.background2);
return true;
case R.id.buttonfour:
iv.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
buildDialog(iv);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void buildDialog(final ImageView v){
//create AlertDialog.Builder object
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//customize dialog
builder.setTitle("Pick an Image!");
builder.setMessage("Please Select Image One or Image Two:");
builder.setCancelable(false);
builder.setPositiveButton("image1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//be passed a object of type DialogInterface and an Id represents which button was clicked
v.setImageResource(R.drawable.image1);
}
});
builder.setNegativeButton("image2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
v.setImageResource(R.drawable.image2);
}
});
//finally buidler.show()
builder.show();
}
}
about the keyword final:
A final variable cannot be given a new value after it has been assigned one.
A final method cannot be overridden.
a final class cannot be extended.