一个Demo让你掌握Android所有控件

简介: 原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士"       下面给出实现各个组件的源代码: 1.
原文: 一个Demo让你掌握Android所有控件


本文是转载收藏,侵删,出处:"安卓巴士"



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


0.jpg



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

1.下拉框实现--Spinner

  1. package com.cellcom;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.Button;  
  12. import android.widget.Spinner;  
  13.   
  14. //下拉框  
  15. public class SpinnerActivity extends Activity {  
  16.   
  17.     private Spinner spinner1;  
  18.     private Spinner spinner2;  
  19.     private Button ok;  
  20.     private ArrayAdapter countiesAdapter;  
  21.     private String[] mCounties={"beijing","guangdong","guangxi","hunan"};  
  22.     private List<String> allCounties=new ArrayList<String>();  
  23.     private String result="你选择的是:";  
  24.       
  25.       
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         // TODO Auto-generated method stub  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.spinner);  
  31.           
  32.         spinner1=(Spinner)findViewById(R.id.spinner1);  
  33.         spinner2=(Spinner)findViewById(R.id.spinner2);  
  34.         ok=(Button)findViewById(R.id.ok);  
  35.           
  36.         for(int i=0;i<mCounties.length;i++){  
  37.             allCounties.add(mCounties[i]);  
  38.         }  
  39.           
  40.         countiesAdapter=new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,allCounties);  
  41.         countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  42.         spinner1.setAdapter(countiesAdapter);  
  43.           
  44.         ArrayAdapter adapter=ArrayAdapter.createFromResource(SpinnerActivity.this,R.array.counties,android.R.layout.simple_spinner_item);  
  45.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  46.         spinner2.setAdapter(adapter);  
  47.           
  48.         //单击第一个下拉按钮时,显示选择的值。   
  49.         spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
  50.             @Override  
  51.             public void onItemSelected(AdapterView<?> adapter, View view,  
  52.                     int position, long id) {  
  53.                 // TODO Auto-generated method stub  
  54.                 String str=(String)spinner1.getAdapter().getItem((int)id);  
  55.                 setTitle(result+str);  
  56.             }  
  57.   
  58.             @Override  
  59.             public void onNothingSelected(AdapterView<?> arg0) {  
  60.                 // TODO Auto-generated method stub  
  61.                   
  62.             }  
  63.         });  
  64.           
  65.         //单击第二个下拉按钮时,显示选择的值。   
  66.         spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
  67.             @Override  
  68.             public void onItemSelected(AdapterView<?> adapter, View view,  
  69.                     int position, long id) {  
  70.                 String str=(String)spinner2.getAdapter().getItem(position);  
  71.                 setTitle(result+str);  
  72.             }  
  73.   
  74.             @Override  
  75.             public void onNothingSelected(AdapterView<?> arg0) {  
  76.             }  
  77.         });  
  78.           
  79.           
  80.           
  81.         //单击确定按钮,提取选择的值.  
  82.         ok.setOnClickListener(new View.OnClickListener() {  
  83.             @Override  
  84.             public void onClick(View v) {  
  85.                 setTitle(result+spinner1.getSelectedItem()+"  - >>  "+spinner2.getSelectedItem());  
  86.             }  
  87.         });  
  88.           
  89.     }  
  90.   
  91. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.app.ProgressDialog;  
  7. import android.content.DialogInterface;  
  8. import android.os.Bundle;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. //对话框测试  
  16. public class AlertDialogActivity extends Activity {  
  17.   
  18.     private Button button1;  
  19.     private Button button2;  
  20.     private Button button3;  
  21.     private Button button4;  
  22.       
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.alert_dialog);  
  27.         setTitle("4种对话框!");  
  28.           
  29.         button1=(Button)findViewById(R.id.button1);  
  30.         button2=(Button)findViewById(R.id.button2);  
  31.         button3=(Button)findViewById(R.id.button3);  
  32.         button4=(Button)findViewById(R.id.button4);  
  33.           
  34.         //第一种对话框  
  35.         button1.setOnClickListener(new View.OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 Builder builder=new AlertDialog.Builder(AlertDialogActivity.this);  
  39.                 builder.setIcon(R.drawable.alert_dialog_icon);  
  40.                 builder.setTitle("哇哈哈!");  
  41.                 builder.setMessage("去不去?");  
  42.                 builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  43.                     @Override  
  44.                     public void onClick(DialogInterface dialog, int which) {  
  45.                         Toast.makeText(AlertDialogActivity.this"你选择了确定按钮!", Toast.LENGTH_SHORT).show();  
  46.                     }  
  47.                 });  
  48.                 builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  49.                     @Override  
  50.                     public void onClick(DialogInterface dialog, int which) {  
  51.                         Toast.makeText(AlertDialogActivity.this"你选择了取消按钮!", Toast.LENGTH_SHORT).show();  
  52.                     }  
  53.                 });  
  54.                 builder.show();  
  55.             }  
  56.         });  
  57.           
  58.         //第二种对话框  
  59.         button2.setOnClickListener(new View.OnClickListener() {  
  60.             @Override  
  61.             public void onClick(View v) {  
  62.                 new AlertDialog.Builder(AlertDialogActivity.this)  
  63.                 .setIcon(R.drawable.alert_dialog_icon)  
  64.                 .setTitle("温馨提示")  
  65.                 .setMessage("提示内容:三个按钮")  
  66.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  67.                     @Override  
  68.                     public void onClick(DialogInterface dialog, int which) {  
  69.                         Toast.makeText(AlertDialogActivity.this"你选择了确定按钮!", Toast.LENGTH_SHORT).show();  
  70.                     }  
  71.                 })  
  72.                 .setNeutralButton("详情"new DialogInterface.OnClickListener() {  
  73.                     @Override  
  74.                     public void onClick(DialogInterface dialog, int which) {  
  75.                         Toast.makeText(AlertDialogActivity.this"你选择了详情按钮!", Toast.LENGTH_SHORT).show();  
  76.                     }  
  77.                 })  
  78.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  79.                     @Override  
  80.                     public void onClick(DialogInterface dialog, int which) {  
  81.                         Toast.makeText(AlertDialogActivity.this"你选择了取消按钮!", Toast.LENGTH_SHORT).show();  
  82.                     }  
  83.                 })  
  84.                 .show();  
  85.             }  
  86.         });  
  87.           
  88.         //第三种按钮  
  89.         button3.setOnClickListener(new View.OnClickListener() {  
  90.             @Override  
  91.             public void onClick(View v) {  
  92.                 LayoutInflater inflater=LayoutInflater.from(AlertDialogActivity.this);  
  93.                 final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null);  
  94.                   
  95.                 final EditText usernameET=(EditText)textEntryView.findViewById(R.id.username_value);  
  96.                 final EditText passwordET=(EditText)textEntryView.findViewById(R.id.password_value);  
  97.                 //final String username=usernameET.getText().toString();  
  98.                   
  99.                 new AlertDialog.Builder(AlertDialogActivity.this)  
  100.                 .setIcon(R.drawable.alert_dialog_icon)  
  101.                 .setTitle("温馨提醒")  
  102.                 .setView(textEntryView)  
  103.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  104.                     @Override  
  105.                     public void onClick(DialogInterface dialog, int which) {  
  106.                         Toast.makeText(AlertDialogActivity.this"用户名="+usernameET.getText().toString()+"\n密码="+passwordET.getText().toString(), Toast.LENGTH_LONG).show();  
  107.                     }  
  108.                 })  
  109.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  110.                     @Override  
  111.                     public void onClick(DialogInterface dialog, int which) {  
  112.                         Toast.makeText(AlertDialogActivity.this"你选择了确定取消!", Toast.LENGTH_SHORT).show();  
  113.                     }  
  114.                 })  
  115.                 .show();  
  116.             }  
  117.         });  
  118.           
  119.         //第四种对话框  
  120.         button4.setOnClickListener(new View.OnClickListener() {  
  121.             @Override  
  122.             public void onClick(View v) {  
  123.                 ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this);  
  124.                 dialog.setTitle("处理中。。。");  
  125.                 dialog.setMessage("请稍后。。。");  
  126.                 dialog.show();  
  127.             }  
  128.         });  
  129.     }  
  130. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.ArrayAdapter;  
  7. import android.widget.AutoCompleteTextView;  
  8. import android.widget.Button;  
  9.   
  10. //自动提示框  
  11. public class AutoCompleteTextViewActivity extends Activity {  
  12.   
  13.     private AutoCompleteTextView autoComplete;  
  14.     private Button cleanButton;  
  15.     static final String[] COUNTRIES = new String[] {  
  16.         "China" ,"Russia""Germany",  
  17.         "Ukraine""Belarus""USA" ,"China1" ,"China12""Germany",  
  18.         "Russia2""Belarus""USA" ,"UAA","UBC","UBB","CCC","BBB","广州1","广州2","广州3","广州4","广东1","广东2","广东3",  
  19.         };  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.auto_complete_text_view);  
  26.         setTitle("AutoCompleteTextView示例!");  
  27.         autoComplete=(AutoCompleteTextView)findViewById(R.id.auto_complete);  
  28.         cleanButton=(Button)findViewById(R.id.cleanButton);  
  29.           
  30.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(AutoCompleteTextViewActivity.this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);  
  31.         autoComplete.setAdapter(adapter);  
  32.           
  33.         //清空  
  34.         cleanButton.setOnClickListener(new View.OnClickListener() {  
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 autoComplete.setText("");  
  38.             }  
  39.         });  
  40.     }  
  41.       
  42. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.RadioButton;  
  10.   
  11. /** 
  12.  * @author nwang 
  13.  *  
  14.  * 输入身高和选择性别,计算标准体重。并把数据返回到原来Activity 
  15.  */  
  16. public class Bundle1Activity extends Activity {  
  17.   
  18.     private int my_requestCode=1550;  
  19.     private RadioButton sexMan;  
  20.     private RadioButton sexWoman;  
  21.     private EditText heightEdit;  
  22.     private Button okButton;  
  23.       
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.           
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.bundle1);  
  29.           
  30.         sexMan=(RadioButton)findViewById(R.id.sex_man);  
  31.         sexWoman=(RadioButton)findViewById(R.id.sex_woman);  
  32.         heightEdit=(EditText)findViewById(R.id.height_edit);  
  33.         okButton=(Button)findViewById(R.id.button_ok);  
  34.           
  35.         okButton.setOnClickListener(new View.OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 double height=Double.parseDouble(heightEdit.getText().toString());  
  39.                 String sex="";  
  40.                 if(sexMan.isChecked()){  
  41.                     sex="M";  
  42.                 }else{  
  43.                     sex="F";  
  44.                 }  
  45.                   
  46.                 Intent intent=new Intent();  
  47.                 intent.setClass(Bundle1Activity.this, Bundle2Activity.class);  
  48.                 //封装数据  
  49.                 Bundle bundle=new Bundle();  
  50.                 bundle.putDouble("height", height);  
  51.                 bundle.putString("sex", sex);  
  52.                 intent.putExtras(bundle);  
  53.                 startActivityForResult(intent, my_requestCode);  
  54.             }  
  55.         });  
  56.     }  
  57.       
  58.     //回调方法  
  59.     @Override  
  60.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  61.         super.onActivityResult(requestCode, resultCode, data);  
  62.           
  63.         switch (requestCode) {  
  64.         case RESULT_OK:  
  65.             //RESULT_OK表示返回成功,提取数据。  
  66.             Bundle bundle=data.getExtras();  
  67.             String sex=bundle.getString("sex");  
  68.             double height=bundle.getDouble("height");  
  69.             heightEdit.setText(String.valueOf(height));  
  70.             if(sex.equals("M")){  
  71.                 sexMan.setChecked(true);  
  72.             }else{  
  73.                 sexWoman.setChecked(true);  
  74.             }  
  75.             break;  
  76.   
  77.         default:  
  78.             break;  
  79.         }  
  80.     }  
  81.       
  82. }  
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;
		}
	}
	
}

  1. package com.cellcom;  
  2.   
  3. import java.text.DecimalFormat;  
  4. import java.text.NumberFormat;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.   
  13. public class Bundle2Activity extends Activity{  
  14.   
  15.     private TextView result;  
  16.     private Button backButton;  
  17.     Intent intent=null;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         // TODO Auto-generated method stub  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.bundle2);  
  23.         result=(TextView)findViewById(R.id.result);  
  24.         backButton=(Button)findViewById(R.id.button_back);  
  25.           
  26.         intent=this.getIntent();  
  27.         Bundle bundle=intent.getExtras();  
  28.         double height=bundle.getDouble("height");  
  29.         String sex=bundle.getString("sex");  
  30.         String sexStr="";  
  31.         if("M".equals(sex)){  
  32.             sexStr="男性";  
  33.         }else{  
  34.             sexStr="女性";  
  35.         }  
  36.         String weight=this.getWeight(sex, height);  
  37.         result.setText("你是一位:"+sexStr+"\n身高是:"+height+"厘米 \n你的标准体重:"+weight+"公斤");  
  38.           
  39.         //返回上一页  
  40.         backButton.setOnClickListener(new View.OnClickListener() {  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.                 Bundle2Activity.this.setResult(RESULT_OK, intent);    
  44.                 Bundle2Activity.this.finish();  
  45.             }  
  46.         });  
  47.     }  
  48.       
  49.     public String getWeight(String sex,double height){  
  50.         String weight="";  
  51.         if("M".endsWith(sex)){  
  52.             weight=format((height-80)*0.7);  
  53.         }else{  
  54.             weight=format((height-70)*0.6);  
  55.         }  
  56.         return weight;  
  57.     }  
  58.       
  59.     public String format(double num){  
  60.         NumberFormat formatter=new DecimalFormat("0.00");  
  61.         String s=formatter.format(num);  
  62.         return s;  
  63.     }  
  64. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.CheckBox;  
  8.   
  9. public class CheckBoxActivity extends Activity {  
  10.   
  11.     private CheckBox plainCB;  
  12.     private CheckBox serifCB;  
  13.     private CheckBox boldCB;  
  14.     private CheckBox italicCB;  
  15.     private Button button1;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.check_box);  
  21.           
  22.         plainCB=(CheckBox)findViewById(R.id.plain_cb);  
  23.         serifCB=(CheckBox)findViewById(R.id.serif_cb);  
  24.         boldCB=(CheckBox)findViewById(R.id.bold_cb);  
  25.         italicCB=(CheckBox)findViewById(R.id.italic_cb);  
  26.         button1=(Button)findViewById(R.id.button1);  
  27.           
  28.         button1.setOnClickListener(new View.OnClickListener() {  
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 String result="";  
  32.                 if(plainCB.isChecked()){  
  33.                     result+=plainCB.getText()+"、";  
  34.                 }  
  35.                 if(serifCB.isChecked()){  
  36.                     result+=serifCB.getText()+"、";  
  37.                 }  
  38.                 if(boldCB.isChecked()){  
  39.                     result+=boldCB.getText()+"、";  
  40.                 }  
  41.                 if(italicCB.isChecked()){  
  42.                     result+=italicCB.getText()+"、";  
  43.                 }  
  44.                 if(!"".equals(result)){  
  45.                     result=result.substring(0,result.length()-1);  
  46.                 }else{  
  47.                     result="你没有选择!!!";  
  48.                 }  
  49.                 setTitle("Checked:"+result);  
  50.             }  
  51.         });  
  52.           
  53.     }  
  54.   
  55. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.view.ContextMenu;  
  7. import android.view.Menu;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.view.ContextMenu.ContextMenuInfo;  
  11. import android.widget.TextView;  
  12.   
  13. public class ContextMenuActivity extends Activity {  
  14.   
  15.     private final static int ITEM0=Menu.FIRST;  
  16.     private final static int ITEM1=Menu.FIRST+1;  
  17.     private final static int ITEM2=Menu.FIRST+2;  
  18.     private TextView tv;  
  19.       
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         // TODO Auto-generated method stub  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.context_menu);  
  25.         setTitle("单击Menu键看到效果!");  
  26.         tv=(TextView)findViewById(R.id.tv1);  
  27.           
  28.         this.registerForContextMenu(tv);//将上下文注册到TextView  
  29.     }  
  30.       
  31.     @Override  
  32.     public void onCreateContextMenu(ContextMenu menu, View v,  
  33.             ContextMenuInfo menuInfo) {  
  34.         menu.add(0, ITEM0, 0"红色背景");  
  35.         menu.add(0,ITEM1,0,"绿色背景");  
  36.         menu.add(0,ITEM2,0,"白色背景");  
  37.           
  38.     }  
  39.       
  40.     @Override  
  41.     public boolean onContextItemSelected(MenuItem item) {  
  42.         switch (item.getItemId()) {  
  43.         case ITEM0:  
  44.             tv.setTextColor(Color.RED);  
  45.             break;  
  46.         case ITEM1:  
  47.             tv.setTextColor(Color.GREEN);  
  48.             break;  
  49.         case ITEM2:  
  50.             tv.setTextColor(Color.WHITE);  
  51.             break;  
  52.         default:  
  53.             break;  
  54.         }  
  55.         return true;  
  56.     }  
  57.       
  58. }  
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

  1. package com.cellcom;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.DatePicker;  
  11. import android.widget.TimePicker;  
  12.   
  13. //DatePicker日期选择器、TimePicker时间选择器 使用  
  14. public class DatePickerActivity extends Activity {  
  15.   
  16.     private Button datePickerButton;  
  17.     private DatePicker datePicker1;  
  18.     private TimePicker timePicker1;  
  19.       
  20.     //默认生成系统当前时间  
  21.     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");        
  22.     String str=sdf.format(new Date());  
  23.     int year=Integer.parseInt(str.substring(04));  
  24.     int month=Integer.parseInt(str.substring(5,7))-1;  
  25.     int day=Integer.parseInt(str.substring(8,10));  
  26.     int hour=Integer.parseInt(str.substring(11,13));  
  27.     int minute=Integer.parseInt(str.substring(14,16));  
  28.       
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         // TODO Auto-generated method stub  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.date_picker);  
  34.         setTitle("DatePicker、TimePicker示例!");  
  35.           
  36.         datePicker1=(DatePicker)findViewById(R.id.datePicker1);  
  37.         timePicker1=(TimePicker)findViewById(R.id.timePicker1);  
  38.         datePickerButton=(Button)findViewById(R.id.datePickerButton1);  
  39.   
  40.         datePicker1.init(year,month,day, null);  
  41.           
  42.         timePicker1.setCurrentHour(hour);  
  43.         timePicker1.setCurrentMinute(minute);  
  44.           
  45.         timePicker1.setIs24HourView(true);  
  46.           
  47.         datePickerButton.setOnClickListener(new View.OnClickListener() {  
  48.             @Override  
  49.             public void onClick(View v) {  
  50.                 setTitle("你选择的是:"+datePicker1.getYear()+"年"+datePicker1.getMonth()+"月"+datePicker1.getDayOfMonth()+"日 "+timePicker1.getCurrentHour()+"小时"+timePicker1.getCurrentMinute()+"分");  
  51.             }  
  52.         });  
  53.     }  
  54.   
  55. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.GridView;  
  10. import android.widget.ImageView;  
  11.   
  12. //网络视图控件  
  13. public class GridViewActivity extends Activity {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.grid_view);  
  20.         setTitle("网络视图控件GridView!");  
  21.           
  22.         GridView gridView=(GridView)findViewById(R.id.grid_view);  
  23.         gridView.setAdapter(new ImageAdapter(this));  
  24.           
  25.     }  
  26.       
  27.     public class ImageAdapter extends BaseAdapter{  
  28.   
  29.         private Context mContext;  
  30.         public ImageAdapter(Context context){  
  31.             mContext=context;  
  32.         }  
  33.         @Override  
  34.         public int getCount() {  
  35.             // TODO Auto-generated method stub  
  36.             return mImageIds.length;  
  37.         }  
  38.   
  39.         @Override  
  40.         public Object getItem(int position) {  
  41.             // TODO Auto-generated method stub  
  42.             return null;  
  43.         }  
  44.   
  45.         @Override  
  46.         public long getItemId(int position) {  
  47.             // TODO Auto-generated method stub  
  48.             return 0;  
  49.         }  
  50.   
  51.         @Override  
  52.         public View getView(int position, View convertView, ViewGroup parent) {  
  53.             ImageView imageView;  
  54.             if(convertView==null){  
  55.                 imageView=new ImageView(mContext);  
  56.                 imageView.setLayoutParams(new GridView.LayoutParams(85,85));  
  57.                 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  
  58.                 imageView.setPadding(8888);  
  59.             }else{  
  60.                 imageView=(ImageView)convertView;  
  61.             }  
  62.             imageView.setImageResource(mImageIds[position]);  
  63.             return imageView;  
  64.         }  
  65.           
  66.     }  
  67.       
  68.       
  69.       
  70.     private Integer[] mImageIds = {  
  71.             R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,  
  72.             R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,  
  73.             R.drawable.sample_6, R.drawable.sample_7,R.drawable.sample_8,  
  74.             R.drawable.sample_9,R.drawable.sample_10,R.drawable.sample_11};  
  75.   
  76. }  
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

  1. package com.cellcom;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.ProgressBar;  
  8.   
  9. import android.widget.TextView;  
  10.   
  11. /** 
  12.  *  
  13.  * @author nwang 
  14.  *  
  15.  * 进度条ProgressBar与线程使用。单击开始按钮,进度条水平方向前进! 
  16.  * 
  17.  */  
  18. public class ProgressBarHandlerActivity extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.       
  21.     private Button startButton;  
  22.     private Button stopButton;  
  23.     private TextView result;  
  24.     private ProgressBar firstProgressBar;  
  25.     private final static int defaultValue=10;  
  26.     private int i=0;  
  27.       
  28.       
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.progress_bar);  
  33.         setTitle("ProgressBar使用!");  
  34.         startButton=(Button)findViewById(R.id.startButton);  
  35.         stopButton=(Button)findViewById(R.id.stopButton);  
  36.         result=(TextView)findViewById(R.id.result);  
  37.         firstProgressBar=(ProgressBar)findViewById(R.id.firstProgressBar);  
  38.           
  39.         firstProgressBar.setVisibility(0);//刚开始设置进度条可见  
  40.           
  41.         startButton.setOnClickListener(new View.OnClickListener() {  
  42.               
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 /*i+=defaultValue; 
  46.                 firstProgressBar.setProgress(i); 
  47.                 firstProgressBar.setSecondaryProgress(i+defaultValue);*/  
  48.                 result.setText(((double)i/firstProgressBar.getMax()*100)+"%");  
  49.                 handler.post(progressBarThread);  
  50.             }  
  51.         });  
  52.           
  53.         stopButton.setOnClickListener(new View.OnClickListener() {  
  54.               
  55.             @Override  
  56.             public void onClick(View v) {  
  57.                 handler.removeCallbacks(progressBarThread);           
  58.             }  
  59.         });  
  60.     }  
  61.       
  62.     //创建Handler对象  
  63.     Handler handler=new Handler();  
  64.       
  65.     //创建线程对象  
  66.     MyRunnable progressBarThread=new MyRunnable();  
  67.     class MyRunnable implements Runnable{  
  68.         @Override  
  69.         public void run() {  
  70.             i+=defaultValue;  
  71.             handler.postDelayed(progressBarThread, 1000);  
  72.             firstProgressBar.setProgress(i);  
  73.             firstProgressBar.setSecondaryProgress(i+defaultValue);  
  74.             result.setText(((double)i/firstProgressBar.getMax()*100)+"%");  
  75.             if(i>=firstProgressBar.getMax()) i=0;  
  76.               
  77.         }  
  78.           
  79.     }  
  80.       
  81. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.RadioButton;  
  8.   
  9. //单选控件  
  10. public class RadioGroupActivity extends Activity {  
  11.   
  12.     private RadioButton radioFemale;  
  13.     private RadioButton radioMale;  
  14.     private Button ok;  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.           
  21.         setContentView(R.layout.radio_group);  
  22.           
  23.         radioFemale=(RadioButton)findViewById(R.id.radioFemale);  
  24.         radioMale=(RadioButton)findViewById(R.id.radioMale);  
  25.         ok=(Button)findViewById(R.id.ok);  
  26.           
  27.         ok.setOnClickListener(new View.OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 String result="你选择的是:";  
  31.                 if(radioFemale.isChecked()){  
  32.                     setTitle(result+"女");  
  33.                 }else{  
  34.                     setTitle(result+"男");  
  35.                 }  
  36.             }  
  37.         });  
  38.     }  
  39.   
  40. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.ImageButton;  
  7. import android.widget.RatingBar;  
  8. import android.widget.Toast;  
  9.   
  10. //评分组件RatingBar、ImageView图片、 ImageButton图片按钮  
  11. public class RatingBarActivity extends Activity {  
  12.   
  13.     private RatingBar ratingBar;  
  14.     private ImageButton imageButton;  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.rating_bar);  
  21.         setTitle("评分组件RatingBar、ImageView图片、 ImageButton图片按钮");  
  22.         ratingBar=(RatingBar)findViewById(R.id.rating_bar);  
  23.         imageButton=(ImageButton)findViewById(R.id.imageButton);  
  24.           
  25.         imageButton.setOnClickListener(new View.OnClickListener() {  
  26.               
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 Toast.makeText(RatingBarActivity.this"单击图片按钮!!", Toast.LENGTH_LONG).show();  
  30.             }  
  31.         });  
  32.     }  
  33. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.widget.TabHost;  
  7.   
  8. //标签控件Tab  
  9. public class TabDemoActivity extends TabActivity {  
  10.   
  11.       
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setTitle("标签控件Tab");  
  15.         TabHost tabHost=getTabHost();  
  16.         LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(),true);  
  17.         tabHost.addTab(tabHost.newTabSpec("biaoqian1").setIndicator("标签tab1").setContent(R.id.tab1));  
  18.         tabHost.addTab(tabHost.newTabSpec("biaoqian2").setIndicator("标签tab2").setContent(R.id.tab2));  
  19.         tabHost.addTab(tabHost.newTabSpec("biaoqian3").setIndicator("标签tab3").setContent(R.id.tab3));  
  20.     }     
  21. }  
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

  1. package com.cellcom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.SubMenu;  
  8.   
  9. //子菜单subMenu  
  10. public class SubMenuActivity extends Activity {  
  11.   
  12.     private final static int ITEM_NEW_FILE=Menu.FIRST;  
  13.     private final static int ITEM_OPEN_FILE=Menu.FIRST+1;  
  14.     private final static int ITEM_COPY=Menu.FIRST+2;  
  15.     private final static int ITEM_CLOSE=Menu.FIRST+3;  
  16.     private final static int ITEM_SAVE=Menu.FIRST+4;  
  17.     private final static int ITEM_SAVE_ALL=Menu.FIRST+5;  
  18.     private final static int ITEM_CUT=Menu.FIRST+6;  
  19.     private final static int ITEM_PASTE=Menu.FIRST+7;  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.sub_menu);  
  26.         setTitle("单击Menu键看到效果!");  
  27.     }  
  28.       
  29.     @Override  
  30.     public boolean onCreateOptionsMenu(Menu menu) {  
  31.         SubMenu file=menu.addSubMenu("文件");  
  32.         SubMenu edit=menu.addSubMenu("编辑");  
  33.           
  34.         file.add(0,ITEM_NEW_FILE,0,"新建");  
  35.         file.add(0,ITEM_OPEN_FILE,0,"打开");  
  36.         file.add(0,ITEM_CLOSE,0,"关闭");  
  37.         file.add(0,ITEM_SAVE,0,"保存");  
  38.         file.add(0,ITEM_SAVE_ALL,0,"保存全部");  
  39.           
  40.           
  41.         edit.add(0,ITEM_COPY,0,"复制");  
  42.         edit.add(0,ITEM_CUT,0,"剪切");  
  43.         edit.add(0,ITEM_PASTE,0,"粘贴");  
  44.           
  45.         return true;  
  46.     }  
  47.       
  48.     @Override  
  49.     public boolean onOptionsItemSelected(MenuItem item) {  
  50.         switch (item.getItemId()) {  
  51.         case ITEM_NEW_FILE:  
  52.             setTitle("新建文件!");  
  53.             break;  
  54.         case ITEM_OPEN_FILE:  
  55.             setTitle("打开文件");  
  56.             break;  
  57.         case ITEM_COPY:  
  58.             setTitle("复制文件");  
  59.             break;  
  60.         default:  
  61.             break;  
  62.         }  
  63.         return true;  
  64.     }  
  65. }  
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/a1042185842b/9484637

目录
相关文章
|
4月前
|
XML API Android开发
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
本文介绍了如何使用androidx.preference库快速创建具有一级和二级菜单的Android设置界面的步骤和示例代码。
144 1
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
|
3月前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
2月前
|
XML 存储 Java
浅谈Android的TextView控件
浅谈Android的TextView控件
47 0
|
3月前
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
58 3
|
4月前
|
前端开发 Android开发 开发者
安卓开发中的自定义视图:构建你的第一个控件
【8月更文挑战第26天】在安卓开发的浩瀚海洋中,自定义视图是一块充满魔力的乐土。它不仅是开发者展示创造力的舞台,更是实现独特用户体验的关键。本文将带你步入自定义视图的世界,从基础概念到实战应用,一步步教你如何打造自己的第一个控件。无论你是初学者还是有经验的开发者,这篇文章都将为你的开发之旅增添新的风景。
|
6月前
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
104 4
|
6月前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
228 2
|
6月前
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
84 2
|
5月前
|
XML 数据格式
Android-自定义三角形评分控件
Android-自定义三角形评分控件
48 0
|
6月前
|
XML Android开发 数据格式
Android基础控件介绍
Android基础控件介绍