(3)Button、Menu and Dialog

(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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值