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

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


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



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


0.jpg



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

1.下拉框实现--Spinner

[java] view plaincopyprint?
  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="你选择的是:";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onItemSelected(AdapterView<?> adapter, View view,int position, long id) {// TODO Auto-generated method stubString str=(String)spinner1.getAdapter().getItem((int)id);setTitle(result+str);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});//单击第二个下拉按钮时,显示选择的值。 spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> adapter, View view,int position, long id) {String str=(String)spinner2.getAdapter().getItem(position);setTitle(result+str);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}});//单击确定按钮,提取选择的值.ok.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {setTitle(result+spinner1.getSelectedItem()+"  - >>  "+spinner2.getSelectedItem());}});}}

2.对话框的实现--AlertDialog

[java] view plaincopyprint?
  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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();}});builder.show();}});//第二种对话框button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new AlertDialog.Builder(AlertDialogActivity.this).setIcon(R.drawable.alert_dialog_icon).setTitle("温馨提示").setMessage("提示内容:三个按钮").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();}}).setNeutralButton("详情", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "你选择了详情按钮!", Toast.LENGTH_SHORT).show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();}}).show();}});//第三种按钮button3.setOnClickListener(new View.OnClickListener() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(AlertDialogActivity.this, "你选择了确定取消!", Toast.LENGTH_SHORT).show();}}).show();}});//第四种对话框button4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this);dialog.setTitle("处理中。。。");dialog.setMessage("请稍后。。。");dialog.show();}});}
}

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

[java] view plaincopyprint?
  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",};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View v) {autoComplete.setText("");}});}}

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

[java] view plaincopyprint?
  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;@Overrideprotected 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() {@Overridepublic 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);}});}//回调方法@Overrideprotected 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;}}}

[java] view plaincopyprint?
  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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic 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

[java] view plaincopyprint?
  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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic 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

[java] view plaincopyprint?
  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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.context_menu);setTitle("单击Menu键看到效果!");tv=(TextView)findViewById(R.id.tv1);this.registerForContextMenu(tv);//将上下文注册到TextView}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {menu.add(0, ITEM0, 0, "红色背景");menu.add(0,ITEM1,0,"绿色背景");menu.add(0,ITEM2,0,"白色背景");}@Overridepublic 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

[java] view plaincopyprint?
  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));@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View v) {setTitle("你选择的是:"+datePicker1.getYear()+"年"+datePicker1.getMonth()+"月"+datePicker1.getDayOfMonth()+"日 "+timePicker1.getCurrentHour()+"小时"+timePicker1.getCurrentMinute()+"分");}});}}

8.网格视图控件--GridView

[java] view plaincopyprint?
  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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mImageIds.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic 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

[java] view plaincopyprint?
  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;@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {handler.removeCallbacks(progressBarThread);			}});}//创建Handler对象Handler handler=new Handler();//创建线程对象MyRunnable progressBarThread=new MyRunnable();class MyRunnable implements Runnable{@Overridepublic 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

[java] view plaincopyprint?
  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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View v) {String result="你选择的是:";if(radioFemale.isChecked()){setTitle(result+"女");}else{setTitle(result+"男");}}});}}

11.打分组件--RatingBar

[java] view plaincopyprint?
  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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View v) {Toast.makeText(RatingBarActivity.this, "单击图片按钮!!", Toast.LENGTH_LONG).show();}});}
}

12.标签控件--Tab

[java] view plaincopyprint?
  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

[java] view plaincopyprint?
  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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.sub_menu);setTitle("单击Menu键看到效果!");}@Overridepublic 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;}@Overridepublic 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

posted on 2017-09-11 09:14 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/7503387.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/282424.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

九妹带你走向 架构师

迈向系统架构师编者按&#xff1a;系统架构师是许多程序员的梦想职业。今天的你也许已经掌握了各种开发工具&#xff0c;并且能够使用各种平台进行开发&#xff0c;但作为一个架构师的要求&#xff0c;也许还有很长的道路。邢波涛先生在LAMP架构上的造诣&#xff0c;让我邀请他…

WPF 使用 DrawingContext 绘制温度计

WPF 使用 DrawingContext 绘制温度计控件名&#xff1a;Thermometer作者&#xff1a; WPFDevelopersOrg原文链接&#xff1a; https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用大于等于.NET40&#xff1b;Visual Studio 2022;项目使用 MIT 开源许可协议&#xff…

装win10系统

一、使用U盘介质安装win10系统&#xff08;官方方式&#xff09; 官方安装工具下载地址&#xff1a;https://www.microsoft.com/zh-cn/software-download/windows10 1、进入官方安装工具下载页面&#xff0c;点击立即下载工具&#xff0c;下载安装工具。2、下载完成后&#xff…

C#构造函数、操作符重载以及自定义类型转换

构造器 构造器&#xff08;构造函数&#xff09;是将类型的实例初始化的特殊方法。构造器可分为实例构造器和类型构造器&#xff0c;本节将详细介绍有关内容。 实例构造器 顾名思义&#xff0c;实例构造器的作用就是对类型的实例进行初始化。如果类没有显示定义任何构造器&…

「Dotnet 工具箱」 自动生成并绑定 Https 证书

这里是 Dotnet 工具箱&#xff0c;定期分享 Dotnet 有趣&#xff0c;有用的工具&#xff0c;不要忘记关注。介绍LettuceEncrypt 是一个使用 C# 开发的免费的工具&#xff0c;它和证书颁发机构 &#xff08;CA&#xff09;集成&#xff0c;比如 Lets Encrypt&#xff0c;它使用了…

1115: 零起点学算法22——华氏摄氏温度转换

1115: 零起点学算法22——华氏摄氏温度转换 Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 3522 Accepted: 1456[Submit][Status][Web Board]Description 输入一个华氏温度&#xff0c;根据公式C(5/9)(F-32)计算对应的摄氏温度。 Input 输入一个…

Navicat Premium 12 的安装破解

Navicat 这款软件可以说 是数据库可视化操作的神器, 有绿色的 (最原始版本, 好像现在已经不维护了) , 有金色的 (改良收费版 ) , 还有彩色的 (最新版) , 这里 , 推荐使用彩色版 (也就是截止目前最新的版本 12.0.27). 操作的话, 感觉相比于小绿和小金有很大改进 , 很棒 , 在此给…

Vuejs——组件——slot内容分发

2019独角兽企业重金招聘Python工程师标准>>> ①概述&#xff1a; 简单来说&#xff0c;假如父组件需要在子组件内放一些DOM&#xff0c;那么这些DOM是显示、不显示、在哪个地方显示、如何显示&#xff0c;就是slot分发负责的活。 ②默认情况下 父组件在子组件内套的…

turtle库基础练习

画一组同切圆 import turtleturtle.shape(turtle)turtle.circle(10) turtle.circle(20) turtle.circle(30) turtle.circle(40) turtle.circle(50) turtle.circle(60) turtle.circle(70) turtle.circle(80)turtle.hideturtle() turtle.done() 画一组同心圆 import turtleturtle.…

检查你的项目的引用包依赖关系

2019独角兽企业重金招聘Python工程师标准>>> 随着着开发的进展,你的项目越来越大,引用的第三方包越来越多,但如何查看都依赖了哪些包,甚至传递依赖又是怎样? 首先解决这个问题的前提,你的项目需要是maven项目,然后可以做如下设置: 选中项目&#xff0c;右键->ru…

git 项目操作

1 创建本地仓库,克隆远程项目代码到本地仓库2. 当我们在本地写了一些代码之后 , 查看本地仓库状态3. 提交改变到待提交区 git add .4. 提交代码到待推送区 git commit -m "新建项目kuman"5. 将本地代码推送到远程代码仓库 git push origin master:nanle 注: 将本地m…

(二)SpringBoot功能

web开发 spring boot web开发非常的简单&#xff0c;其中包括常用的json输出、filters、property、log等 json 接口开发 在以前的spring 开发的时候需要我们提供json接口的时候需要做那些配置呢 就这样我们会经常由于配置错误&#xff0c;导致406错误等等&#xff0c;spring bo…

----斐波那契数列---eval函数----类递归思想 栈 进出 思想

------------ 斐波那契 数列 ---------------【1&#xff0c;1,2,3,5,8,13,21,34&#xff0c;...】 1 列表方法实现 # l[1,1] # # # while len(l)<20: # # l.append(l[-1]l[-2]) # # print(l) # # while len(l)!4: # l.append(l[-1]l[-2]) # print(l) # 2 …

HybridTime - Accessible Global Consistency with High Clock Uncertainty

Amazon’s Dynamo [9] and Facebook’s Cassandra [13], relax the consistency model&#xff0c;and offer only eventual consistency. Others such as HBase [1] and BigTable [4] offer strong consistency only for operations touching a single partition, but not acr…

公司目前实行的git团队协作方案

1. git init 新建本地仓库2. git clone 项目地址 获取远程master代码3. 在本地master代码上进行开发, 并将修改提交到待推送区4. 开发完, 在本地master分支基础上创建ready分支5. 在本地ready分支上(本地测试分支), 拉取并合并远程nanle分支最新代码(远程测试分支)6. 将本地re…

bzoj3122 [Sdoi2013]随机数生成器(bsgs+扩欧+数列)

Description Input 输入含有多组数据&#xff0c;第一行一个正整数T&#xff0c;表示这个测试点内的数据组数。 接下来T行&#xff0c;每行有五个整数p&#xff0c;a&#xff0c;b&#xff0c;X1&#xff0c;t&#xff0c;表示一组数据。保证X1和t都是合法的页码。 注意&…

挑选合适自己的一门编程语言

2019独角兽企业重金招聘Python工程师标准>>> 导读想学编程的原因有很多&#xff0c;你也许是想要做一个程序&#xff0c;又或者你只是想投身于这个行业&#xff0c;所以&#xff0c;在选择你的第一门编程语言之前&#xff0c;问问你自己&#xff1a;你想要在哪里运行…

css 实现章节名称不换行,多余部分用 ... 代替

修改之前:修改之后: 代码: <p style "white-space: nowrap;text-overflow: ellipsis;overflow: hidden;"><? $d[name] ?></p> <i><? $d[pen_name] ?></i> <i><?phpforeach ($d[tags] as $t) {echo $t[tag_name];…

.NET 反向代理-YARP 部署Https(SSL)

相关文章&#xff1a;.NET 反向代理-YARP.NET 反向代理-YARP 根据域名转发分享一个基于Abp 和Yarp 开发的API网关项目使用 Yarp 做网关YARP&#xff08;Yet Another Reverse Proxy&#xff09;是使用 .NET 构建的高度可定制的反向代理C# 开源一个基于 yarp 的 API 网关 Demo&am…

shell脚本--cut命令

bash&shell系列文章&#xff1a;http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.1 选项说明 cut命令将行按指定的分隔符分割成多列&#xff0c;它的弱点在于不好处理多个分隔符重复的情况&#xff0c;因此经常结合tr的压缩功能。 -b&#xff1a;按字节筛选&#xff…