Android的简略组件总结

Android的简单组件总结

常用组件的总结:

提示框的使用:

	new AlertDialog.Builder(this)   
	.setTitle("title")   
	.setMessage("bla..bla")   
	.setPositiveButton("OK", new DialogInterface.OnClickListener() {   
	@Override   
	public void onClick(DialogInterface dialog, int which) {   
	}   
	})   
	.show();  

 

 

自动提示输入框的使用:

			// 通过ID查找到main.xml中的AutoCompleteTextView控件
			autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocompleteTv);

			// 设定一个Array适配器,将数组数据放入适配器中
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line, item);

			// 对AutoCompleteTextView进行适配
			autoCompleteTextView.setAdapter(adapter);
			
			//设置AutoCompleteTextView的监听器
			autoCompleteTextView.setOnItemClickListener(new OnItemClickListener()
			{

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3)
				{
					String str = "这次妖精把" + autoCompleteTextView.getText().toString()
							+ "抓住了!";
					updateText(str);
					
				}
			});
			
			// 通过ID查找到main.xml中的MultiAutoCompleteTextView控件
			multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
			
			// 对MultiAutoCompleteTextView进行适配
			multiAutoCompleteTextView.setAdapter(adapter);
			
			//设置分隔符,默认的是逗号(,)
			multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());   
			
			//设置MultiAutoCompleteTextView的监听器
			multiAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener()
			{

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3)
				{
					String str = "这次妖精把" + multiAutoCompleteTextView.getText().toString()
							+ "抓住了!";
					updateText(str);
					
				}
			});

 

多个复选框的使用:

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chkgrp_layout);

		// 通过ID查找到main.xml中的RadioButton控件
		tangseng= (CheckBox) findViewById(R.id.tangsengchk);

		wukong= (CheckBox) findViewById(R.id.sunwukongchk);

		bajie= (CheckBox) findViewById(R.id.zhubajiechk);

		shazeng= (CheckBox) findViewById(R.id.shaheshangchk);
		// 通过ID查找到main.xml中的Button控件
		button = (Button) findViewById(R.id.mpbtn);
		text = (TextView) findViewById(R.id.mtvInfo);
		// 为Button控件增加单击监听器
		button.setOnClickListener(this);
	}

	@Override  
	  public void onClick(View v)  
	    {  
	        String str = "";  
	        //唐僧单选框被选中   
	        if(tangseng.isChecked()){  
	            str += "唐僧~";  
	        }  
	        //悟空单选框被选中   
	        else if(wukong.isChecked()){  
	            str += "悟空~";  
	        }  
	        //八戒单选框被选中   
	        else if(bajie.isChecked()){  
	            str += "八戒~";  
	        }  
	        //沙僧单选框被选中   
	        else if(shazeng.isChecked()){  
	            str += "沙僧~";  
	        }  
	        //没有人被选中   
	        else if(str.equals("")){  
	            str += "没有人";  
	        }  
	        str +="被妖精抓走了!";  
	        updateText(str);  
	          
	}  

 

单选按钮组的使用:

/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radiogrp_layout);

		// 通过ID查找到main.xml中的RadioButton控件
		tangseng= (RadioButton) findViewById(R.id.tangseng);

		wukong= (RadioButton) findViewById(R.id.sunwukong);

		bajie= (RadioButton) findViewById(R.id.zhubajie);

		shazeng= (RadioButton) findViewById(R.id.shaheshang);
		
		
		// 通过ID查找到main.xml中的Button控件
		Button button = (Button) findViewById(R.id.pbtn);
		text= (TextView) findViewById(R.id.tvInfo);
		// 为Button控件增加单击监听器
		button.setOnClickListener(this);
	}

	@Override  
	  public void onClick(View v)  
	    {  
	        String str = "";  
	        //唐僧单选框被选中   
	        if(tangseng.isChecked()){  
	            str += "唐僧~";  
	        }  
	        //悟空单选框被选中   
	        if(wukong.isChecked()){  
	            str += "悟空~";  
	        }  
	        //八戒单选框被选中   
	        if(bajie.isChecked()){  
	            str += "八戒~";  
	        }  
	        //沙僧单选框被选中   
	        if(shazeng.isChecked()){  
	            str += "沙僧~";  
	        }  
	        //没有人被选中   
	        if(str.equals("")){  
	            str += "没有人";  
	        }  
	        str +="被妖精抓走了!";  
	          
	        updateText(str);  
	          
	}  

 

时间选择器的使用:

@Override
	    protected Dialog onCreateDialog(int id) {   
	        //用来获取日期和时间的   
	        Calendar calendar = Calendar.getInstance();       
	  
	        Dialog dialog = null;   
	        switch(id) {   
	            case DATE_DIALOG:   
	                DatePickerDialog.OnDateSetListener dateListener =   
	                    new DatePickerDialog.OnDateSetListener() {   
	                        @Override   
	                        public void onDateSet(DatePicker datePicker,   
	                                int year, int month, int dayOfMonth) {   
	                            EditText editText =   
	                                (EditText) findViewById(R.id.editText);   
	                             //Calendar月份是从0开始,所以month要加1   
	                            editText.setText("你选择了" + year + "年" +   
	                                    (month+1) + "月" + dayOfMonth + "日");   
	                        }   
	                    };   
	                dialog = new DatePickerDialog(this,   
	                        dateListener,   
	                        calendar.get(Calendar.YEAR),   
	                        calendar.get(Calendar.MONTH),   
	                        calendar.get(Calendar.DAY_OF_MONTH));   
	                break;   
	            case TIME_DIALOG:   
	                TimePickerDialog.OnTimeSetListener timeListener =   
	                    new TimePickerDialog.OnTimeSetListener() {      
	  
	                        @Override   
	                        public void onTimeSet(TimePicker timerPicker,   
	                                int hourOfDay, int minute) {   
	                            EditText editText =   
	                                (EditText) findViewById(R.id.editText);   
	                            editText.setText("你选择了" + hourOfDay + "时" +   
	                                     minute + "分");   
	                        }   
	                    };   
	                    dialog = new TimePickerDialog(this, timeListener,   
	                            calendar.get(Calendar.HOUR_OF_DAY),   
	                            calendar.get(Calendar.MINUTE),   
	                            false);   //是否为二十四制   
	                break;   
	            default:   
	                break;   
	        }   
	        return dialog;   
	    }   

 缩放工具:

 @Override
	 protected void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.main);
      //设置标题
	  setTitle("ZoomControls");
	  zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
	  text = (TextView) findViewById(R.id.text);
	  //点击放大按钮事件
	  zoomControls.setOnZoomInClickListener(new OnClickListener() {

	   @Override
	   public void onClick(View v) {
	    size = size + 2;
	    text.setTextSize(size);
	   }
	  });
	  //点击缩小的按钮的事件
	  zoomControls.setOnZoomOutClickListener(new OnClickListener() {

	   @Override
	   public void onClick(View v) {

	    size = size - 2;
	    text.setTextSize(size);
	   }

	  });

 

抽屉功能:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:orientation="vertical" android:background="@drawable/default_bg">

 <SlidingDrawer android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:handle="@+id/handle"
  android:content="@+id/content" android:orientation="vertical"
  android:id="@+id/slidingdrawer">

  <ImageButton android:id="@id/handle" android:layout_width="50dip"
   android:layout_height="44dip" android:src="@drawable/up" />

  <LinearLayout android:id="@id/content"
   android:layout_width="fill_parent" android:layout_height="fill_parent" 
   android:background="#ffffff">

   <TextView android:text="这是一个滑动式抽屉的示例" 
   android:id="@+id/tv"
   android:textSize="18px"
   android:textColor="#000000"
   android:gravity="center_vertical|center_horizontal"
   android:layout_width="match_parent" 
   android:textStyle="bold"
   android:layout_height="match_parent"></TextView>
  </LinearLayout>

 </SlidingDrawer>
</LinearLayout>

 

 

 @Override
	 protected void onCreate(Bundle savedInstanceState) {
		  // TODO Auto-generated method stub
		  super.onCreate(savedInstanceState);
		  setContentView(R.layout.main);
		  imbg=(ImageButton)findViewById(R.id.handle);
		  mDrawer=(SlidingDrawer)findViewById(R.id.slidingdrawer);
		  tv=(TextView)findViewById(R.id.tv);
		  /**
		   * 抽屜打開事件
		   */
		  mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()
		  {
			   @Override
			   public void onDrawerOpened() {
			    flag=true;
			    imbg.setImageResource(R.drawable.down);
			   }
		  });
		  /**
		   * 抽屜關閉事件
		   */
		  mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener(){
			   @Override
			   public void onDrawerClosed() {
			    flag=false;
			    imbg.setImageResource(R.drawable.up);
			   }
		  });
		  /**
		   * 抽屜拖動事件
		   */
		  mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener(){
			   @Override
			   public void onScrollEnded() {
			    tv.setText("结束拖动");
			   }
			   @Override
			   public void onScrollStarted() {
			   tv.setText("开始拖动");
			   }
		   
		  });

	 
	 }