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

时间:2022-12-28 21:52:30

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



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

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

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

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

1.下拉框实现--Spinner

  1. package com.cellcom;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.Button;
  10. import android.widget.Spinner;
  11. //下拉框
  12. public class SpinnerActivity extends Activity {
  13. private Spinner spinner1;
  14. private Spinner spinner2;
  15. private Button ok;
  16. private ArrayAdapter countiesAdapter;
  17. private String[] mCounties={"beijing","guangdong","guangxi","hunan"};
  18. private List<String> allCounties=new ArrayList<String>();
  19. private String result="你选择的是:";
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. // TODO Auto-generated method stub
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.spinner);
  25. spinner1=(Spinner)findViewById(R.id.spinner1);
  26. spinner2=(Spinner)findViewById(R.id.spinner2);
  27. ok=(Button)findViewById(R.id.ok);
  28. for(int i=0;i<mCounties.length;i++){
  29. allCounties.add(mCounties[i]);
  30. }
  31. countiesAdapter=new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,allCounties);
  32. countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  33. spinner1.setAdapter(countiesAdapter);
  34. ArrayAdapter adapter=ArrayAdapter.createFromResource(SpinnerActivity.this,R.array.counties,android.R.layout.simple_spinner_item);
  35. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  36. spinner2.setAdapter(adapter);
  37. //单击第一个下拉按钮时,显示选择的值。
  38. spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  39. @Override
  40. public void onItemSelected(AdapterView<?> adapter, View view,
  41. int position, long id) {
  42. // TODO Auto-generated method stub
  43. String str=(String)spinner1.getAdapter().getItem((int)id);
  44. setTitle(result+str);
  45. }
  46. @Override
  47. public void onNothingSelected(AdapterView<?> arg0) {
  48. // TODO Auto-generated method stub
  49. }
  50. });
  51. //单击第二个下拉按钮时,显示选择的值。
  52. spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  53. @Override
  54. public void onItemSelected(AdapterView<?> adapter, View view,
  55. int position, long id) {
  56. String str=(String)spinner2.getAdapter().getItem(position);
  57. setTitle(result+str);
  58. }
  59. @Override
  60. public void onNothingSelected(AdapterView<?> arg0) {
  61. }
  62. });
  63. //单击确定按钮,提取选择的值.
  64. ok.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. setTitle(result+spinner1.getSelectedItem()+"  - >>  "+spinner2.getSelectedItem());
  68. }
  69. });
  70. }
  71. }
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. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.AlertDialog.Builder;
  5. import android.app.ProgressDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. //对话框测试
  14. public class AlertDialogActivity extends Activity {
  15. private Button button1;
  16. private Button button2;
  17. private Button button3;
  18. private Button button4;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.alert_dialog);
  23. setTitle("4种对话框!");
  24. button1=(Button)findViewById(R.id.button1);
  25. button2=(Button)findViewById(R.id.button2);
  26. button3=(Button)findViewById(R.id.button3);
  27. button4=(Button)findViewById(R.id.button4);
  28. //第一种对话框
  29. button1.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. Builder builder=new AlertDialog.Builder(AlertDialogActivity.this);
  33. builder.setIcon(R.drawable.alert_dialog_icon);
  34. builder.setTitle("哇哈哈!");
  35. builder.setMessage("去不去?");
  36. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  37. @Override
  38. public void onClick(DialogInterface dialog, int which) {
  39. Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
  40. }
  41. });
  42. builder.setNegativeButton("取消", 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.show();
  49. }
  50. });
  51. //第二种对话框
  52. button2.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. new AlertDialog.Builder(AlertDialogActivity.this)
  56. .setIcon(R.drawable.alert_dialog_icon)
  57. .setTitle("温馨提示")
  58. .setMessage("提示内容:三个按钮")
  59. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  60. @Override
  61. public void onClick(DialogInterface dialog, int which) {
  62. Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
  63. }
  64. })
  65. .setNeutralButton("详情", new DialogInterface.OnClickListener() {
  66. @Override
  67. public void onClick(DialogInterface dialog, int which) {
  68. Toast.makeText(AlertDialogActivity.this, "你选择了详情按钮!", Toast.LENGTH_SHORT).show();
  69. }
  70. })
  71. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  72. @Override
  73. public void onClick(DialogInterface dialog, int which) {
  74. Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
  75. }
  76. })
  77. .show();
  78. }
  79. });
  80. //第三种按钮
  81. button3.setOnClickListener(new View.OnClickListener() {
  82. @Override
  83. public void onClick(View v) {
  84. LayoutInflater inflater=LayoutInflater.from(AlertDialogActivity.this);
  85. final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null);
  86. final EditText usernameET=(EditText)textEntryView.findViewById(R.id.username_value);
  87. final EditText passwordET=(EditText)textEntryView.findViewById(R.id.password_value);
  88. //final String username=usernameET.getText().toString();
  89. new AlertDialog.Builder(AlertDialogActivity.this)
  90. .setIcon(R.drawable.alert_dialog_icon)
  91. .setTitle("温馨提醒")
  92. .setView(textEntryView)
  93. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  94. @Override
  95. public void onClick(DialogInterface dialog, int which) {
  96. Toast.makeText(AlertDialogActivity.this, "用户名="+usernameET.getText().toString()+"\n密码="+passwordET.getText().toString(), Toast.LENGTH_LONG).show();
  97. }
  98. })
  99. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  100. @Override
  101. public void onClick(DialogInterface dialog, int which) {
  102. Toast.makeText(AlertDialogActivity.this, "你选择了确定取消!", Toast.LENGTH_SHORT).show();
  103. }
  104. })
  105. .show();
  106. }
  107. });
  108. //第四种对话框
  109. button4.setOnClickListener(new View.OnClickListener() {
  110. @Override
  111. public void onClick(View v) {
  112. ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this);
  113. dialog.setTitle("处理中。。。");
  114. dialog.setMessage("请稍后。。。");
  115. dialog.show();
  116. }
  117. });
  118. }
  119. }
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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.ArrayAdapter;
  6. import android.widget.AutoCompleteTextView;
  7. import android.widget.Button;
  8. //自动提示框
  9. public class AutoCompleteTextViewActivity extends Activity {
  10. private AutoCompleteTextView autoComplete;
  11. private Button cleanButton;
  12. static final String[] COUNTRIES = new String[] {
  13. "China" ,"Russia", "Germany",
  14. "Ukraine", "Belarus", "USA" ,"China1" ,"China12", "Germany",
  15. "Russia2", "Belarus", "USA" ,"UAA","UBC","UBB","CCC","BBB","广州1","广州2","广州3","广州4","广东1","广东2","广东3",
  16. };
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.auto_complete_text_view);
  22. setTitle("AutoCompleteTextView示例!");
  23. autoComplete=(AutoCompleteTextView)findViewById(R.id.auto_complete);
  24. cleanButton=(Button)findViewById(R.id.cleanButton);
  25. ArrayAdapter<String> adapter=new ArrayAdapter<String>(AutoCompleteTextViewActivity.this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);
  26. autoComplete.setAdapter(adapter);
  27. //清空
  28. cleanButton.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. autoComplete.setText("");
  32. }
  33. });
  34. }
  35. }
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. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. /**
  10. * @author nwang
  11. *
  12. * 输入身高和选择性别,计算标准体重。并把数据返回到原来Activity
  13. */
  14. public class Bundle1Activity extends Activity {
  15. private int my_requestCode=1550;
  16. private RadioButton sexMan;
  17. private RadioButton sexWoman;
  18. private EditText heightEdit;
  19. private Button okButton;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.bundle1);
  24. sexMan=(RadioButton)findViewById(R.id.sex_man);
  25. sexWoman=(RadioButton)findViewById(R.id.sex_woman);
  26. heightEdit=(EditText)findViewById(R.id.height_edit);
  27. okButton=(Button)findViewById(R.id.button_ok);
  28. okButton.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. double height=Double.parseDouble(heightEdit.getText().toString());
  32. String sex="";
  33. if(sexMan.isChecked()){
  34. sex="M";
  35. }else{
  36. sex="F";
  37. }
  38. Intent intent=new Intent();
  39. intent.setClass(Bundle1Activity.this, Bundle2Activity.class);
  40. //封装数据
  41. Bundle bundle=new Bundle();
  42. bundle.putDouble("height", height);
  43. bundle.putString("sex", sex);
  44. intent.putExtras(bundle);
  45. startActivityForResult(intent, my_requestCode);
  46. }
  47. });
  48. }
  49. //回调方法
  50. @Override
  51. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  52. super.onActivityResult(requestCode, resultCode, data);
  53. switch (requestCode) {
  54. case RESULT_OK:
  55. //RESULT_OK表示返回成功,提取数据。
  56. Bundle bundle=data.getExtras();
  57. String sex=bundle.getString("sex");
  58. double height=bundle.getDouble("height");
  59. heightEdit.setText(String.valueOf(height));
  60. if(sex.equals("M")){
  61. sexMan.setChecked(true);
  62. }else{
  63. sexWoman.setChecked(true);
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
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. import java.text.DecimalFormat;
  3. import java.text.NumberFormat;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. public class Bundle2Activity extends Activity{
  11. private TextView result;
  12. private Button backButton;
  13. Intent intent=null;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.bundle2);
  19. result=(TextView)findViewById(R.id.result);
  20. backButton=(Button)findViewById(R.id.button_back);
  21. intent=this.getIntent();
  22. Bundle bundle=intent.getExtras();
  23. double height=bundle.getDouble("height");
  24. String sex=bundle.getString("sex");
  25. String sexStr="";
  26. if("M".equals(sex)){
  27. sexStr="男性";
  28. }else{
  29. sexStr="女性";
  30. }
  31. String weight=this.getWeight(sex, height);
  32. result.setText("你是一位:"+sexStr+"\n身高是:"+height+"厘米 \n你的标准体重:"+weight+"公斤");
  33. //返回上一页
  34. backButton.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. Bundle2Activity.this.setResult(RESULT_OK, intent);
  38. Bundle2Activity.this.finish();
  39. }
  40. });
  41. }
  42. public String getWeight(String sex,double height){
  43. String weight="";
  44. if("M".endsWith(sex)){
  45. weight=format((height-80)*0.7);
  46. }else{
  47. weight=format((height-70)*0.6);
  48. }
  49. return weight;
  50. }
  51. public String format(double num){
  52. NumberFormat formatter=new DecimalFormat("0.00");
  53. String s=formatter.format(num);
  54. return s;
  55. }
  56. }
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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.CheckBox;
  7. public class CheckBoxActivity extends Activity {
  8. private CheckBox plainCB;
  9. private CheckBox serifCB;
  10. private CheckBox boldCB;
  11. private CheckBox italicCB;
  12. private Button button1;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. // TODO Auto-generated method stub
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.check_box);
  18. plainCB=(CheckBox)findViewById(R.id.plain_cb);
  19. serifCB=(CheckBox)findViewById(R.id.serif_cb);
  20. boldCB=(CheckBox)findViewById(R.id.bold_cb);
  21. italicCB=(CheckBox)findViewById(R.id.italic_cb);
  22. button1=(Button)findViewById(R.id.button1);
  23. button1.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. String result="";
  27. if(plainCB.isChecked()){
  28. result+=plainCB.getText()+"、";
  29. }
  30. if(serifCB.isChecked()){
  31. result+=serifCB.getText()+"、";
  32. }
  33. if(boldCB.isChecked()){
  34. result+=boldCB.getText()+"、";
  35. }
  36. if(italicCB.isChecked()){
  37. result+=italicCB.getText()+"、";
  38. }
  39. if(!"".equals(result)){
  40. result=result.substring(0,result.length()-1);
  41. }else{
  42. result="你没有选择!!!";
  43. }
  44. setTitle("Checked:"+result);
  45. }
  46. });
  47. }
  48. }
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. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.ContextMenu;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.ContextMenu.ContextMenuInfo;
  10. import android.widget.TextView;
  11. public class ContextMenuActivity extends Activity {
  12. private final static int ITEM0=Menu.FIRST;
  13. private final static int ITEM1=Menu.FIRST+1;
  14. private final static int ITEM2=Menu.FIRST+2;
  15. private TextView tv;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. // TODO Auto-generated method stub
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.context_menu);
  21. setTitle("单击Menu键看到效果!");
  22. tv=(TextView)findViewById(R.id.tv1);
  23. this.registerForContextMenu(tv);//将上下文注册到TextView
  24. }
  25. @Override
  26. public void onCreateContextMenu(ContextMenu menu, View v,
  27. ContextMenuInfo menuInfo) {
  28. menu.add(0, ITEM0, 0, "红色背景");
  29. menu.add(0,ITEM1,0,"绿色背景");
  30. menu.add(0,ITEM2,0,"白色背景");
  31. }
  32. @Override
  33. public boolean onContextItemSelected(MenuItem item) {
  34. switch (item.getItemId()) {
  35. case ITEM0:
  36. tv.setTextColor(Color.RED);
  37. break;
  38. case ITEM1:
  39. tv.setTextColor(Color.GREEN);
  40. break;
  41. case ITEM2:
  42. tv.setTextColor(Color.WHITE);
  43. break;
  44. default:
  45. break;
  46. }
  47. return true;
  48. }
  49. }
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. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.DatePicker;
  9. import android.widget.TimePicker;
  10. //DatePicker日期选择器、TimePicker时间选择器 使用
  11. public class DatePickerActivity extends Activity {
  12. private Button datePickerButton;
  13. private DatePicker datePicker1;
  14. private TimePicker timePicker1;
  15. //默认生成系统当前时间
  16. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
  17. String str=sdf.format(new Date());
  18. int year=Integer.parseInt(str.substring(0, 4));
  19. int month=Integer.parseInt(str.substring(5,7))-1;
  20. int day=Integer.parseInt(str.substring(8,10));
  21. int hour=Integer.parseInt(str.substring(11,13));
  22. int minute=Integer.parseInt(str.substring(14,16));
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. // TODO Auto-generated method stub
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.date_picker);
  28. setTitle("DatePicker、TimePicker示例!");
  29. datePicker1=(DatePicker)findViewById(R.id.datePicker1);
  30. timePicker1=(TimePicker)findViewById(R.id.timePicker1);
  31. datePickerButton=(Button)findViewById(R.id.datePickerButton1);
  32. datePicker1.init(year,month,day, null);
  33. timePicker1.setCurrentHour(hour);
  34. timePicker1.setCurrentMinute(minute);
  35. timePicker1.setIs24HourView(true);
  36. datePickerButton.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. setTitle("你选择的是:"+datePicker1.getYear()+"年"+datePicker1.getMonth()+"月"+datePicker1.getDayOfMonth()+"日 "+timePicker1.getCurrentHour()+"小时"+timePicker1.getCurrentMinute()+"分");
  40. }
  41. });
  42. }
  43. }
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. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.GridView;
  9. import android.widget.ImageView;
  10. //网络视图控件
  11. public class GridViewActivity extends Activity {
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.grid_view);
  17. setTitle("网络视图控件GridView!");
  18. GridView gridView=(GridView)findViewById(R.id.grid_view);
  19. gridView.setAdapter(new ImageAdapter(this));
  20. }
  21. public class ImageAdapter extends BaseAdapter{
  22. private Context mContext;
  23. public ImageAdapter(Context context){
  24. mContext=context;
  25. }
  26. @Override
  27. public int getCount() {
  28. // TODO Auto-generated method stub
  29. return mImageIds.length;
  30. }
  31. @Override
  32. public Object getItem(int position) {
  33. // TODO Auto-generated method stub
  34. return null;
  35. }
  36. @Override
  37. public long getItemId(int position) {
  38. // TODO Auto-generated method stub
  39. return 0;
  40. }
  41. @Override
  42. public View getView(int position, View convertView, ViewGroup parent) {
  43. ImageView imageView;
  44. if(convertView==null){
  45. imageView=new ImageView(mContext);
  46. imageView.setLayoutParams(new GridView.LayoutParams(85,85));
  47. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  48. imageView.setPadding(8, 8, 8, 8);
  49. }else{
  50. imageView=(ImageView)convertView;
  51. }
  52. imageView.setImageResource(mImageIds[position]);
  53. return imageView;
  54. }
  55. }
  56. private Integer[] mImageIds = {
  57. R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
  58. R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
  59. R.drawable.sample_6, R.drawable.sample_7,R.drawable.sample_8,
  60. R.drawable.sample_9,R.drawable.sample_10,R.drawable.sample_11};
  61. }
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. import android.widget.TextView;
  9. /**
  10. *
  11. * @author nwang
  12. *
  13. * 进度条ProgressBar与线程使用。单击开始按钮,进度条水平方向前进!
  14. *
  15. */
  16. public class ProgressBarHandlerActivity extends Activity {
  17. /** Called when the activity is first created. */
  18. private Button startButton;
  19. private Button stopButton;
  20. private TextView result;
  21. private ProgressBar firstProgressBar;
  22. private final static int defaultValue=10;
  23. private int i=0;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.progress_bar);
  28. setTitle("ProgressBar使用!");
  29. startButton=(Button)findViewById(R.id.startButton);
  30. stopButton=(Button)findViewById(R.id.stopButton);
  31. result=(TextView)findViewById(R.id.result);
  32. firstProgressBar=(ProgressBar)findViewById(R.id.firstProgressBar);
  33. firstProgressBar.setVisibility(0);//刚开始设置进度条可见
  34. startButton.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. /*i+=defaultValue;
  38. firstProgressBar.setProgress(i);
  39. firstProgressBar.setSecondaryProgress(i+defaultValue);*/
  40. result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
  41. handler.post(progressBarThread);
  42. }
  43. });
  44. stopButton.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. handler.removeCallbacks(progressBarThread);
  48. }
  49. });
  50. }
  51. //创建Handler对象
  52. Handler handler=new Handler();
  53. //创建线程对象
  54. MyRunnable progressBarThread=new MyRunnable();
  55. class MyRunnable implements Runnable{
  56. @Override
  57. public void run() {
  58. i+=defaultValue;
  59. handler.postDelayed(progressBarThread, 1000);
  60. firstProgressBar.setProgress(i);
  61. firstProgressBar.setSecondaryProgress(i+defaultValue);
  62. result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
  63. if(i>=firstProgressBar.getMax()) i=0;
  64. }
  65. }
  66. }
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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.RadioButton;
  7. //单选控件
  8. public class RadioGroupActivity extends Activity {
  9. private RadioButton radioFemale;
  10. private RadioButton radioMale;
  11. private Button ok;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.radio_group);
  17. radioFemale=(RadioButton)findViewById(R.id.radioFemale);
  18. radioMale=(RadioButton)findViewById(R.id.radioMale);
  19. ok=(Button)findViewById(R.id.ok);
  20. ok.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. String result="你选择的是:";
  24. if(radioFemale.isChecked()){
  25. setTitle(result+"女");
  26. }else{
  27. setTitle(result+"男");
  28. }
  29. }
  30. });
  31. }
  32. }
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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.ImageButton;
  6. import android.widget.RatingBar;
  7. import android.widget.Toast;
  8. //评分组件RatingBar、ImageView图片、 ImageButton图片按钮
  9. public class RatingBarActivity extends Activity {
  10. private RatingBar ratingBar;
  11. private ImageButton imageButton;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.rating_bar);
  17. setTitle("评分组件RatingBar、ImageView图片、 ImageButton图片按钮");
  18. ratingBar=(RatingBar)findViewById(R.id.rating_bar);
  19. imageButton=(ImageButton)findViewById(R.id.imageButton);
  20. imageButton.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. Toast.makeText(RatingBarActivity.this, "单击图片按钮!!", Toast.LENGTH_LONG).show();
  24. }
  25. });
  26. }
  27. }
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. import android.app.TabActivity;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.widget.TabHost;
  6. //标签控件Tab
  7. public class TabDemoActivity extends TabActivity {
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setTitle("标签控件Tab");
  11. TabHost tabHost=getTabHost();
  12. LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(),true);
  13. tabHost.addTab(tabHost.newTabSpec("biaoqian1").setIndicator("标签tab1").setContent(R.id.tab1));
  14. tabHost.addTab(tabHost.newTabSpec("biaoqian2").setIndicator("标签tab2").setContent(R.id.tab2));
  15. tabHost.addTab(tabHost.newTabSpec("biaoqian3").setIndicator("标签tab3").setContent(R.id.tab3));
  16. }
  17. }
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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.view.SubMenu;
  7. //子菜单subMenu
  8. public class SubMenuActivity extends Activity {
  9. private final static int ITEM_NEW_FILE=Menu.FIRST;
  10. private final static int ITEM_OPEN_FILE=Menu.FIRST+1;
  11. private final static int ITEM_COPY=Menu.FIRST+2;
  12. private final static int ITEM_CLOSE=Menu.FIRST+3;
  13. private final static int ITEM_SAVE=Menu.FIRST+4;
  14. private final static int ITEM_SAVE_ALL=Menu.FIRST+5;
  15. private final static int ITEM_CUT=Menu.FIRST+6;
  16. private final static int ITEM_PASTE=Menu.FIRST+7;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.sub_menu);
  22. setTitle("单击Menu键看到效果!");
  23. }
  24. @Override
  25. public boolean onCreateOptionsMenu(Menu menu) {
  26. SubMenu file=menu.addSubMenu("文件");
  27. SubMenu edit=menu.addSubMenu("编辑");
  28. file.add(0,ITEM_NEW_FILE,0,"新建");
  29. file.add(0,ITEM_OPEN_FILE,0,"打开");
  30. file.add(0,ITEM_CLOSE,0,"关闭");
  31. file.add(0,ITEM_SAVE,0,"保存");
  32. file.add(0,ITEM_SAVE_ALL,0,"保存全部");
  33. edit.add(0,ITEM_COPY,0,"复制");
  34. edit.add(0,ITEM_CUT,0,"剪切");
  35. edit.add(0,ITEM_PASTE,0,"粘贴");
  36. return true;
  37. }
  38. @Override
  39. public boolean onOptionsItemSelected(MenuItem item) {
  40. switch (item.getItemId()) {
  41. case ITEM_NEW_FILE:
  42. setTitle("新建文件!");
  43. break;
  44. case ITEM_OPEN_FILE:
  45. setTitle("打开文件");
  46. break;
  47. case ITEM_COPY:
  48. setTitle("复制文件");
  49. break;
  50. default:
  51. break;
  52. }
  53. return true;
  54. }
  55. }
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

一个Demo让你掌握Android所有控件的更多相关文章

  1. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  2. Android M 控件:Snackbar、Toolbar、TabLayout、NavigationView

    Snackbar Snackbar提供了一个介于Toast和AlertDialog之间轻量级控件,它可以很方便的提供消息的提示和动作反馈.Snackbar的使用与Toast的使用基本相同: Snack ...

  3. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  4. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  5. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  6. 矩阵&comma; 矩阵 &comma; Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  7. Android给控件添加触摸回调

    Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...

  8. Android 基本控件相关知识整理

    Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户.作为一个程序员如何才能开发出友好的图形界 ...

  9. Github上star数超1000的Android列表控件

    Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...

随机推荐

  1. java之并发

    一.线程 在java中多线程执行任务,主要有两种方式,一种是通过继承Thread类,重写run方法,优点是比较方便的创建一个线程,缺点是java中每个类只能有一个基类,所有继承了T火热ad类后,就不能 ...

  2. Mysql 数据库文件存储在哪个目录

    也就是说我在mysql里建了一个叫 ac 的数据库,但是我找不到其存储位置,Mysql里面的数据库是怎么存储的,是否也像sqlserver 那样,有一个日志文件和数据文件? mysql数据库在系统上是 ...

  3. VI 命令学习指南

    ctrl+b 屏幕向后(上)移动一屏 ctrl+f 屏幕向前(下)移动一屏 ctrl+u 屏幕向后(上)移动半屏 ctrl+d 屏幕向前(下)移动半屏

  4. MySQL 数据库操作命令汇总

    此文全部都是基本的数据库语言 1.登陆到mysql >mysql -h hostname -u username -p 然后等待系统提示输入密码即可登陆.如果想在登陆的时候就选择好数据库,可以使 ...

  5. iOS开发之让你的应用&OpenCurlyDoubleQuote;动”起来

    概览在 iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互 式绘图,如何通过核心动画创建基础动画.关键帧动 ...

  6. GetFontResourceInfo 函数&colon; 获取字体文件对应的字体名

    在 win 7, gdi32.dll,找不到 GetFontResourceInfo(), 但能找到 GetFontResourceInfoW(), 用法类似. //----------------- ...

  7. webpack4学习笔记

    执行webpack-dev-server的时候不会自动生成dist打包目录,怪不得公司的项目里面都没用到webpack-dev-server呢 执行webpack的时候会生成dist目录 watch的 ...

  8. 阿里云mysql安装配置(CentOS 7&period;3 64)

    自建目录并且加载yum资源mysql 安装 回车之后竟然出现不可以的情况(原因是原来的镜像里面默认装好了mysql5.7) 然后只能尝试跳过密码登录 #vim /etc/my.cnf 在文档内搜索my ...

  9. Bugku-CTF之flag&period;php(点了login咋没反应)

    Day20 flag.php 地址:http://123.206.87.240:8002/flagphp/   点了login咋没反应 提示:hint 

  10. nginx实践&lpar;四&rpar;之静态资源web服务&lpar;防盗链&rpar;

    防盗链目的 防止资源被盗用 http_refer 主要是判断refer信息,判断请求来源是不是合法身份 语法 实例 参数说明: none表示允许没有代理的头信息过来,blocked表示refer信息不 ...