一个Demo让你掌握所有的android控件


注:这个例子来自“安卓巴士”,仅在此学习,阅读





1.jpg 2.jpg3.jpg 4.jpg6.jpg 7.jpg8.jpg 9.jpg 


0.jpg



下面给出实现各个组件的源代码

1.下拉框实现--Spinner

package com.cellcom;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

//下拉框
public class SpinnerActivity extends Activity {

	private Spinner spinner1;
	private Spinner spinner2;
	private Button ok;
	private ArrayAdapter countiesAdapter;
	private String[] mCounties={"beijing","guangdong","guangxi","hunan"};
	private List<String> allCounties=new ArrayList<String>();
	private String result="你选择的是:";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);
		
		spinner1=(Spinner)findViewById(R.id.spinner1);
		spinner2=(Spinner)findViewById(R.id.spinner2);
		ok=(Button)findViewById(R.id.ok);
		
		for(int i=0;i<mCounties.length;i++){
			allCounties.add(mCounties[i]);
		}
		
		countiesAdapter=new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,allCounties);
		countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(countiesAdapter);
		
		ArrayAdapter adapter=ArrayAdapter.createFromResource(SpinnerActivity.this,R.array.counties,android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner2.setAdapter(adapter);
		
		//单击第一个下拉按钮时,显示选择的值。 
		spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String str=(String)spinner1.getAdapter().getItem((int)id);
				setTitle(result+str);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		//单击第二个下拉按钮时,显示选择的值。 
		spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int position, long id) {
				String str=(String)spinner2.getAdapter().getItem(position);
				setTitle(result+str);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		
		
		//单击确定按钮,提取选择的值.
		ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setTitle(result+spinner1.getSelectedItem()+"  - >>  "+spinner2.getSelectedItem());
			}
		});
		
	}

}

2.对话框的实现--AlertDialog

package com.cellcom;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

//对话框测试
public class AlertDialogActivity extends Activity {

	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_dialog);
		setTitle("4种对话框!");
		
		button1=(Button)findViewById(R.id.button1);
		button2=(Button)findViewById(R.id.button2);
		button3=(Button)findViewById(R.id.button3);
		button4=(Button)findViewById(R.id.button4);
		
		//第一种对话框
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Builder builder=new AlertDialog.Builder(AlertDialogActivity.this);
				builder.setIcon(R.drawable.alert_dialog_icon);
				builder.setTitle("哇哈哈!");
				builder.setMessage("去不去?");
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
					}
				});
				builder.show();
			}
		});
		
		//第二种对话框
		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(AlertDialogActivity.this)
				.setIcon(R.drawable.alert_dialog_icon)
				.setTitle("温馨提示")
				.setMessage("提示内容:三个按钮")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
					}
				})
				.setNeutralButton("详情", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "你选择了详情按钮!", Toast.LENGTH_SHORT).show();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
					}
				})
				.show();
			}
		});
		
		//第三种按钮
		button3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				LayoutInflater inflater=LayoutInflater.from(AlertDialogActivity.this);
				final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null);
				
				final EditText usernameET=(EditText)textEntryView.findViewById(R.id.username_value);
				final EditText passwordET=(EditText)textEntryView.findViewById(R.id.password_value);
				//final String username=usernameET.getText().toString();
				
				new AlertDialog.Builder(AlertDialogActivity.this)
				.setIcon(R.drawable.alert_dialog_icon)
				.setTitle("温馨提醒")
				.setView(textEntryView)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "用户名="+usernameET.getText().toString()+"\n密码="+passwordET.getText().toString(), Toast.LENGTH_LONG).show();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogActivity.this, "你选择了确定取消!", Toast.LENGTH_SHORT).show();
					}
				})
				.show();
			}
		});
		
		//第四种对话框
		button4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this);
				dialog.setTitle("处理中。。。");
				dialog.setMessage("请稍后。。。");
				dialog.show();
			}
		});
	}
}

3.自动提示框的实现--AutoCompleteTextView

package com.cellcom;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

//自动提示框
public class AutoCompleteTextViewActivity extends Activity {

	private AutoCompleteTextView autoComplete;
	private Button cleanButton;
	static final String[] COUNTRIES = new String[] {
		"China" ,"Russia", "Germany",
		"Ukraine", "Belarus", "USA" ,"China1" ,"China12", "Germany",
		"Russia2", "Belarus", "USA" ,"UAA","UBC","UBB","CCC","BBB","广州1","广州2","广州3","广州4","广东1","广东2","广东3",
	    };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.auto_complete_text_view);
		setTitle("AutoCompleteTextView示例!");
		autoComplete=(AutoCompleteTextView)findViewById(R.id.auto_complete);
		cleanButton=(Button)findViewById(R.id.cleanButton);
		
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(AutoCompleteTextViewActivity.this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);
		autoComplete.setAdapter(adapter);
		
		//清空
		cleanButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				autoComplete.setText("");
			}
		});
	}
	
}

4.Activity之间传递数据--Bundle

package com.cellcom;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

/**
 * @author nwang
 * 
 * 输入身高和选择性别,计算标准体重。并把数据返回到原来Activity
 */
public class Bundle1Activity extends Activity {

	private int my_requestCode=1550;
	private RadioButton sexMan;
	private RadioButton sexWoman;
	private EditText heightEdit;
	private Button okButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bundle1);
		
		sexMan=(RadioButton)findViewById(R.id.sex_man);
		sexWoman=(RadioButton)findViewById(R.id.sex_woman);
		heightEdit=(EditText)findViewById(R.id.height_edit);
		okButton=(Button)findViewById(R.id.button_ok);
		
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				double height=Double.parseDouble(heightEdit.getText().toString());
				String sex="";
				if(sexMan.isChecked()){
					sex="M";
				}else{
					sex="F";
				}
				
				Intent intent=new Intent();
				intent.setClass(Bundle1Activity.this, Bundle2Activity.class);
				//封装数据
				Bundle bundle=new Bundle();
				bundle.putDouble("height", height);
				bundle.putString("sex", sex);
				intent.putExtras(bundle);
				startActivityForResult(intent, my_requestCode);
			}
		});
	}
	
	//回调方法
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		switch (requestCode) {
		case RESULT_OK:
			//RESULT_OK表示返回成功,提取数据。
			Bundle bundle=data.getExtras();
			String sex=bundle.getString("sex");
			double height=bundle.getDouble("height");
			heightEdit.setText(String.valueOf(height));
			if(sex.equals("M")){
				sexMan.setChecked(true);
			}else{
				sexWoman.setChecked(true);
			}
			break;

		default:
			break;
		}
	}
	
}

package com.cellcom;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Bundle2Activity extends Activity{

	private TextView result;
	private Button backButton;
	Intent intent=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bundle2);
		result=(TextView)findViewById(R.id.result);
		backButton=(Button)findViewById(R.id.button_back);
		
		intent=this.getIntent();
		Bundle bundle=intent.getExtras();
		double height=bundle.getDouble("height");
		String sex=bundle.getString("sex");
		String sexStr="";
		if("M".equals(sex)){
			sexStr="男性";
		}else{
			sexStr="女性";
		}
		String weight=this.getWeight(sex, height);
		result.setText("你是一位:"+sexStr+"\n身高是:"+height+"厘米 \n你的标准体重:"+weight+"公斤");
		
		//返回上一页
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Bundle2Activity.this.setResult(RESULT_OK, intent);	
				Bundle2Activity.this.finish();
			}
		});
	}
	
	public String getWeight(String sex,double height){
		String weight="";
		if("M".endsWith(sex)){
			weight=format((height-80)*0.7);
		}else{
			weight=format((height-70)*0.6);
		}
		return weight;
	}
	
	public String format(double num){
		NumberFormat formatter=new DecimalFormat("0.00");
		String s=formatter.format(num);
		return s;
	}
}

5.复选框--CheckBox

package com.cellcom;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class CheckBoxActivity extends Activity {

	private CheckBox plainCB;
	private CheckBox serifCB;
	private CheckBox boldCB;
	private CheckBox italicCB;
	private Button button1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_box);
		
		plainCB=(CheckBox)findViewById(R.id.plain_cb);
		serifCB=(CheckBox)findViewById(R.id.serif_cb);
		boldCB=(CheckBox)findViewById(R.id.bold_cb);
		italicCB=(CheckBox)findViewById(R.id.italic_cb);
		button1=(Button)findViewById(R.id.button1);
		
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String result="";
				if(plainCB.isChecked()){
					result+=plainCB.getText()+"、";
				}
				if(serifCB.isChecked()){
					result+=serifCB.getText()+"、";
				}
				if(boldCB.isChecked()){
					result+=boldCB.getText()+"、";
				}
				if(italicCB.isChecked()){
					result+=italicCB.getText()+"、";
				}
				if(!"".equals(result)){
					result=result.substring(0,result.length()-1);
				}else{
					result="你没有选择!!!";
				}
				setTitle("Checked:"+result);
			}
		});
		
	}

}

6.菜单的实现--Menu

package com.cellcom;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;

public class ContextMenuActivity extends Activity {

	private final static int ITEM0=Menu.FIRST;
	private final static int ITEM1=Menu.FIRST+1;
	private final static int ITEM2=Menu.FIRST+2;
	private TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.context_menu);
		setTitle("单击Menu键看到效果!");
		tv=(TextView)findViewById(R.id.tv1);
		
		this.registerForContextMenu(tv);//将上下文注册到TextView
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.add(0, ITEM0, 0, "红色背景");
		menu.add(0,ITEM1,0,"绿色背景");
		menu.add(0,ITEM2,0,"白色背景");
		
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ITEM0:
			tv.setTextColor(Color.RED);
			break;
		case ITEM1:
			tv.setTextColor(Color.GREEN);
			break;
		case ITEM2:
			tv.setTextColor(Color.WHITE);
			break;
		default:
			break;
		}
		return true;
	}
	
}

7.日期选择器--DataTimePicker

package com.cellcom;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

//DatePicker日期选择器、TimePicker时间选择器 使用
public class DatePickerActivity extends Activity {

	private Button datePickerButton;
	private DatePicker datePicker1;
	private TimePicker timePicker1;
	
	//默认生成系统当前时间
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");		
	String str=sdf.format(new Date());
	int year=Integer.parseInt(str.substring(0, 4));
	int month=Integer.parseInt(str.substring(5,7))-1;
	int day=Integer.parseInt(str.substring(8,10));
	int hour=Integer.parseInt(str.substring(11,13));
	int minute=Integer.parseInt(str.substring(14,16));
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.date_picker);
		setTitle("DatePicker、TimePicker示例!");
		
		datePicker1=(DatePicker)findViewById(R.id.datePicker1);
		timePicker1=(TimePicker)findViewById(R.id.timePicker1);
		datePickerButton=(Button)findViewById(R.id.datePickerButton1);

		datePicker1.init(year,month,day, null);
		
		timePicker1.setCurrentHour(hour);
		timePicker1.setCurrentMinute(minute);
		
		timePicker1.setIs24HourView(true);
		
		datePickerButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setTitle("你选择的是:"+datePicker1.getYear()+"年"+datePicker1.getMonth()+"月"+datePicker1.getDayOfMonth()+"日 "+timePicker1.getCurrentHour()+"小时"+timePicker1.getCurrentMinute()+"分");
			}
		});
	}

}

8.网格视图控件--GridView

package com.cellcom;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

//网络视图控件
public class GridViewActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grid_view);
		setTitle("网络视图控件GridView!");
		
		GridView gridView=(GridView)findViewById(R.id.grid_view);
		gridView.setAdapter(new ImageAdapter(this));
		
	}
	
	public class ImageAdapter extends BaseAdapter{

		private Context mContext;
		public ImageAdapter(Context context){
			mContext=context;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mImageIds.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if(convertView==null){
				imageView=new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(85,85));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(8, 8, 8, 8);
			}else{
				imageView=(ImageView)convertView;
			}
			imageView.setImageResource(mImageIds[position]);
			return imageView;
		}
		
	}
	
	
	
	private Integer[] mImageIds = {
            R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
            R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,R.drawable.sample_8,
            R.drawable.sample_9,R.drawable.sample_10,R.drawable.sample_11};

}

9.进度条控件--ProgressBar

package com.cellcom;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import android.widget.TextView;

/**
 * 
 * @author nwang
 * 
 * 进度条ProgressBar与线程使用。单击开始按钮,进度条水平方向前进!
 *
 */
public class ProgressBarHandlerActivity extends Activity {
    /** Called when the activity is first created. */
	
	private Button startButton;
	private Button stopButton;
	private TextView result;
	private ProgressBar firstProgressBar;
	private final static int defaultValue=10;
	private int i=0;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progress_bar);
        setTitle("ProgressBar使用!");
        startButton=(Button)findViewById(R.id.startButton);
        stopButton=(Button)findViewById(R.id.stopButton);
        result=(TextView)findViewById(R.id.result);
        firstProgressBar=(ProgressBar)findViewById(R.id.firstProgressBar);
        
        firstProgressBar.setVisibility(0);//刚开始设置进度条可见
        
        startButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*i+=defaultValue;
				firstProgressBar.setProgress(i);
				firstProgressBar.setSecondaryProgress(i+defaultValue);*/
				result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
				handler.post(progressBarThread);
			}
		});
        
        stopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handler.removeCallbacks(progressBarThread);			
			}
		});
    }
    
    //创建Handler对象
    Handler handler=new Handler();
    
    //创建线程对象
    MyRunnable progressBarThread=new MyRunnable();
    class MyRunnable implements Runnable{
		@Override
		public void run() {
			i+=defaultValue;
			handler.postDelayed(progressBarThread, 1000);
			firstProgressBar.setProgress(i);
			firstProgressBar.setSecondaryProgress(i+defaultValue);
			result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
			if(i>=firstProgressBar.getMax()) i=0;
			
		}
    	
    }
    
}

10.单选控件--RadioGroup

package com.cellcom;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

//单选控件
public class RadioGroupActivity extends Activity {

	private RadioButton radioFemale;
	private RadioButton radioMale;
	private Button ok;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.radio_group);
		
		radioFemale=(RadioButton)findViewById(R.id.radioFemale);
		radioMale=(RadioButton)findViewById(R.id.radioMale);
		ok=(Button)findViewById(R.id.ok);
		
		ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String result="你选择的是:";
				if(radioFemale.isChecked()){
					setTitle(result+"女");
				}else{
					setTitle(result+"男");
				}
			}
		});
	}

}

11.打分组件--RatingBar

package com.cellcom;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Toast;

//评分组件RatingBar、ImageView图片、 ImageButton图片按钮
public class RatingBarActivity extends Activity {

	private RatingBar ratingBar;
	private ImageButton imageButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rating_bar);
		setTitle("评分组件RatingBar、ImageView图片、 ImageButton图片按钮");
		ratingBar=(RatingBar)findViewById(R.id.rating_bar);
		imageButton=(ImageButton)findViewById(R.id.imageButton);
		
		imageButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(RatingBarActivity.this, "单击图片按钮!!", Toast.LENGTH_LONG).show();
			}
		});
	}
}

12.标签控件--Tab

package com.cellcom;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

//标签控件Tab
public class TabDemoActivity extends TabActivity {

	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("标签控件Tab");
		TabHost tabHost=getTabHost();
		LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(),true);
		tabHost.addTab(tabHost.newTabSpec("biaoqian1").setIndicator("标签tab1").setContent(R.id.tab1));
		tabHost.addTab(tabHost.newTabSpec("biaoqian2").setIndicator("标签tab2").setContent(R.id.tab2));
		tabHost.addTab(tabHost.newTabSpec("biaoqian3").setIndicator("标签tab3").setContent(R.id.tab3));
	}	
}

13.子菜单的实现--SubMenu

package com.cellcom;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;

//子菜单subMenu
public class SubMenuActivity extends Activity {

	private final static int ITEM_NEW_FILE=Menu.FIRST;
	private final static int ITEM_OPEN_FILE=Menu.FIRST+1;
	private final static int ITEM_COPY=Menu.FIRST+2;
	private final static int ITEM_CLOSE=Menu.FIRST+3;
	private final static int ITEM_SAVE=Menu.FIRST+4;
	private final static int ITEM_SAVE_ALL=Menu.FIRST+5;
	private final static int ITEM_CUT=Menu.FIRST+6;
	private final static int ITEM_PASTE=Menu.FIRST+7;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_menu);
		setTitle("单击Menu键看到效果!");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu file=menu.addSubMenu("文件");
		SubMenu edit=menu.addSubMenu("编辑");
		
		file.add(0,ITEM_NEW_FILE,0,"新建");
		file.add(0,ITEM_OPEN_FILE,0,"打开");
		file.add(0,ITEM_CLOSE,0,"关闭");
		file.add(0,ITEM_SAVE,0,"保存");
		file.add(0,ITEM_SAVE_ALL,0,"保存全部");
		
		
		edit.add(0,ITEM_COPY,0,"复制");
		edit.add(0,ITEM_CUT,0,"剪切");
		edit.add(0,ITEM_PASTE,0,"粘贴");
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ITEM_NEW_FILE:
			setTitle("新建文件!");
			break;
		case ITEM_OPEN_FILE:
			setTitle("打开文件");
			break;
		case ITEM_COPY:
			setTitle("复制文件");
			break;
		default:
			break;
		}
		return true;
	}
}

注:需要源代码的请留下邮箱地址或点击下方的链接自行下载

http://download.csdn.net/detail/dlutbrucezhang/5061337


posted @ 2013-02-11 15:17  丿黑色丶幽默丨灬  阅读(6355)  评论(2编辑  收藏  举报