本文是转载收藏,侵删,出处:"安卓巴士"
|
下面给出实现各个组件的源代码:
1.下拉框实现--Spinner
- package com.cellcom;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.Spinner;
- //下拉框
- public class SpinnerActivity extends Activity {
- private Spinner spinner1;
- private Spinner spinner2;
- private Button ok;
- private ArrayAdapter countiesAdapter;
- private String[] mCounties={"beijing","guangdong","guangxi","hunan"};
- private List<String> allCounties=new ArrayList<String>();
- private String result="你选择的是:";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spinner);
- spinner1=(Spinner)findViewById(R.id.spinner1);
- spinner2=(Spinner)findViewById(R.id.spinner2);
- ok=(Button)findViewById(R.id.ok);
- for(int i=0;i<mCounties.length;i++){
- allCounties.add(mCounties[i]);
- }
- countiesAdapter=new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,allCounties);
- countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner1.setAdapter(countiesAdapter);
- ArrayAdapter adapter=ArrayAdapter.createFromResource(SpinnerActivity.this,R.array.counties,android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spinner2.setAdapter(adapter);
- //单击第一个下拉按钮时,显示选择的值。
- spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> adapter, View view,
- int position, long id) {
- // TODO Auto-generated method stub
- String str=(String)spinner1.getAdapter().getItem((int)id);
- setTitle(result+str);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- //单击第二个下拉按钮时,显示选择的值。
- spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> adapter, View view,
- int position, long id) {
- String str=(String)spinner2.getAdapter().getItem(position);
- setTitle(result+str);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- });
- //单击确定按钮,提取选择的值.
- ok.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- setTitle(result+spinner1.getSelectedItem()+" - >> "+spinner2.getSelectedItem());
- }
- });
- }
- }
package com.cellcom; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner; //下拉框
public class SpinnerActivity extends Activity { private Spinner spinner1;
private Spinner spinner2;
private Button ok;
private ArrayAdapter countiesAdapter;
private String[] mCounties={"beijing","guangdong","guangxi","hunan"};
private List<String> allCounties=new ArrayList<String>();
private String result="你选择的是:"; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner); spinner1=(Spinner)findViewById(R.id.spinner1);
spinner2=(Spinner)findViewById(R.id.spinner2);
ok=(Button)findViewById(R.id.ok); for(int i=0;i<mCounties.length;i++){
allCounties.add(mCounties[i]);
} countiesAdapter=new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,allCounties);
countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(countiesAdapter); ArrayAdapter adapter=ArrayAdapter.createFromResource(SpinnerActivity.this,R.array.counties,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter); //单击第一个下拉按钮时,显示选择的值。
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
// TODO Auto-generated method stub
String str=(String)spinner1.getAdapter().getItem((int)id);
setTitle(result+str);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub }
}); //单击第二个下拉按钮时,显示选择的值。
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
String str=(String)spinner2.getAdapter().getItem(position);
setTitle(result+str);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}); //单击确定按钮,提取选择的值.
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTitle(result+spinner1.getSelectedItem()+" - >> "+spinner2.getSelectedItem());
}
}); } }
2.对话框的实现--AlertDialog
- package com.cellcom;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- //对话框测试
- public class AlertDialogActivity extends Activity {
- private Button button1;
- private Button button2;
- private Button button3;
- private Button button4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.alert_dialog);
- setTitle("4种对话框!");
- button1=(Button)findViewById(R.id.button1);
- button2=(Button)findViewById(R.id.button2);
- button3=(Button)findViewById(R.id.button3);
- button4=(Button)findViewById(R.id.button4);
- //第一种对话框
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Builder builder=new AlertDialog.Builder(AlertDialogActivity.this);
- builder.setIcon(R.drawable.alert_dialog_icon);
- builder.setTitle("哇哈哈!");
- builder.setMessage("去不去?");
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
- }
- });
- builder.show();
- }
- });
- //第二种对话框
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- new AlertDialog.Builder(AlertDialogActivity.this)
- .setIcon(R.drawable.alert_dialog_icon)
- .setTitle("温馨提示")
- .setMessage("提示内容:三个按钮")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
- }
- })
- .setNeutralButton("详情", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "你选择了详情按钮!", Toast.LENGTH_SHORT).show();
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
- }
- })
- .show();
- }
- });
- //第三种按钮
- button3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- LayoutInflater inflater=LayoutInflater.from(AlertDialogActivity.this);
- final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null);
- final EditText usernameET=(EditText)textEntryView.findViewById(R.id.username_value);
- final EditText passwordET=(EditText)textEntryView.findViewById(R.id.password_value);
- //final String username=usernameET.getText().toString();
- new AlertDialog.Builder(AlertDialogActivity.this)
- .setIcon(R.drawable.alert_dialog_icon)
- .setTitle("温馨提醒")
- .setView(textEntryView)
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "用户名="+usernameET.getText().toString()+"\n密码="+passwordET.getText().toString(), Toast.LENGTH_LONG).show();
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(AlertDialogActivity.this, "你选择了确定取消!", Toast.LENGTH_SHORT).show();
- }
- })
- .show();
- }
- });
- //第四种对话框
- button4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this);
- dialog.setTitle("处理中。。。");
- dialog.setMessage("请稍后。。。");
- dialog.show();
- }
- });
- }
- }
package com.cellcom; import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; //对话框测试
public class AlertDialogActivity extends Activity { private Button button1;
private Button button2;
private Button button3;
private Button button4; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
setTitle("4种对话框!"); button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4); //第一种对话框
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Builder builder=new AlertDialog.Builder(AlertDialogActivity.this);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle("哇哈哈!");
builder.setMessage("去不去?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
}); //第二种对话框
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(AlertDialogActivity.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("温馨提示")
.setMessage("提示内容:三个按钮")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "你选择了确定按钮!", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "你选择了详情按钮!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "你选择了取消按钮!", Toast.LENGTH_SHORT).show();
}
})
.show();
}
}); //第三种按钮
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater=LayoutInflater.from(AlertDialogActivity.this);
final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null); final EditText usernameET=(EditText)textEntryView.findViewById(R.id.username_value);
final EditText passwordET=(EditText)textEntryView.findViewById(R.id.password_value);
//final String username=usernameET.getText().toString(); new AlertDialog.Builder(AlertDialogActivity.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("温馨提醒")
.setView(textEntryView)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "用户名="+usernameET.getText().toString()+"\n密码="+passwordET.getText().toString(), Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "你选择了确定取消!", Toast.LENGTH_SHORT).show();
}
})
.show();
}
}); //第四种对话框
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog dialog=new ProgressDialog(AlertDialogActivity.this);
dialog.setTitle("处理中。。。");
dialog.setMessage("请稍后。。。");
dialog.show();
}
});
}
}
3.自动提示框的实现--AutoCompleteTextView
- package com.cellcom;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- //自动提示框
- public class AutoCompleteTextViewActivity extends Activity {
- private AutoCompleteTextView autoComplete;
- private Button cleanButton;
- static final String[] COUNTRIES = new String[] {
- "China" ,"Russia", "Germany",
- "Ukraine", "Belarus", "USA" ,"China1" ,"China12", "Germany",
- "Russia2", "Belarus", "USA" ,"UAA","UBC","UBB","CCC","BBB","广州1","广州2","广州3","广州4","广东1","广东2","广东3",
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.auto_complete_text_view);
- setTitle("AutoCompleteTextView示例!");
- autoComplete=(AutoCompleteTextView)findViewById(R.id.auto_complete);
- cleanButton=(Button)findViewById(R.id.cleanButton);
- ArrayAdapter<String> adapter=new ArrayAdapter<String>(AutoCompleteTextViewActivity.this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);
- autoComplete.setAdapter(adapter);
- //清空
- cleanButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- autoComplete.setText("");
- }
- });
- }
- }
package com.cellcom; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button; //自动提示框
public class AutoCompleteTextViewActivity extends Activity { private AutoCompleteTextView autoComplete;
private Button cleanButton;
static final String[] COUNTRIES = new String[] {
"China" ,"Russia", "Germany",
"Ukraine", "Belarus", "USA" ,"China1" ,"China12", "Germany",
"Russia2", "Belarus", "USA" ,"UAA","UBC","UBB","CCC","BBB","广州1","广州2","广州3","广州4","广东1","广东2","广东3",
}; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_complete_text_view);
setTitle("AutoCompleteTextView示例!");
autoComplete=(AutoCompleteTextView)findViewById(R.id.auto_complete);
cleanButton=(Button)findViewById(R.id.cleanButton); ArrayAdapter<String> adapter=new ArrayAdapter<String>(AutoCompleteTextViewActivity.this,android.R.layout.simple_dropdown_item_1line,COUNTRIES);
autoComplete.setAdapter(adapter); //清空
cleanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
autoComplete.setText("");
}
});
} }
4.Activity之间传递数据--Bundle
- package com.cellcom;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- /**
- * @author nwang
- *
- * 输入身高和选择性别,计算标准体重。并把数据返回到原来Activity
- */
- public class Bundle1Activity extends Activity {
- private int my_requestCode=1550;
- private RadioButton sexMan;
- private RadioButton sexWoman;
- private EditText heightEdit;
- private Button okButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.bundle1);
- sexMan=(RadioButton)findViewById(R.id.sex_man);
- sexWoman=(RadioButton)findViewById(R.id.sex_woman);
- heightEdit=(EditText)findViewById(R.id.height_edit);
- okButton=(Button)findViewById(R.id.button_ok);
- okButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- double height=Double.parseDouble(heightEdit.getText().toString());
- String sex="";
- if(sexMan.isChecked()){
- sex="M";
- }else{
- sex="F";
- }
- Intent intent=new Intent();
- intent.setClass(Bundle1Activity.this, Bundle2Activity.class);
- //封装数据
- Bundle bundle=new Bundle();
- bundle.putDouble("height", height);
- bundle.putString("sex", sex);
- intent.putExtras(bundle);
- startActivityForResult(intent, my_requestCode);
- }
- });
- }
- //回调方法
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case RESULT_OK:
- //RESULT_OK表示返回成功,提取数据。
- Bundle bundle=data.getExtras();
- String sex=bundle.getString("sex");
- double height=bundle.getDouble("height");
- heightEdit.setText(String.valueOf(height));
- if(sex.equals("M")){
- sexMan.setChecked(true);
- }else{
- sexWoman.setChecked(true);
- }
- break;
- default:
- break;
- }
- }
- }
package com.cellcom; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton; /**
* @author nwang
*
* 输入身高和选择性别,计算标准体重。并把数据返回到原来Activity
*/
public class Bundle1Activity extends Activity { private int my_requestCode=1550;
private RadioButton sexMan;
private RadioButton sexWoman;
private EditText heightEdit;
private Button okButton; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.bundle1); sexMan=(RadioButton)findViewById(R.id.sex_man);
sexWoman=(RadioButton)findViewById(R.id.sex_woman);
heightEdit=(EditText)findViewById(R.id.height_edit);
okButton=(Button)findViewById(R.id.button_ok); okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double height=Double.parseDouble(heightEdit.getText().toString());
String sex="";
if(sexMan.isChecked()){
sex="M";
}else{
sex="F";
} Intent intent=new Intent();
intent.setClass(Bundle1Activity.this, Bundle2Activity.class);
//封装数据
Bundle bundle=new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
intent.putExtras(bundle);
startActivityForResult(intent, my_requestCode);
}
});
} //回调方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); switch (requestCode) {
case RESULT_OK:
//RESULT_OK表示返回成功,提取数据。
Bundle bundle=data.getExtras();
String sex=bundle.getString("sex");
double height=bundle.getDouble("height");
heightEdit.setText(String.valueOf(height));
if(sex.equals("M")){
sexMan.setChecked(true);
}else{
sexWoman.setChecked(true);
}
break; default:
break;
}
} }
- package com.cellcom;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class Bundle2Activity extends Activity{
- private TextView result;
- private Button backButton;
- Intent intent=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.bundle2);
- result=(TextView)findViewById(R.id.result);
- backButton=(Button)findViewById(R.id.button_back);
- intent=this.getIntent();
- Bundle bundle=intent.getExtras();
- double height=bundle.getDouble("height");
- String sex=bundle.getString("sex");
- String sexStr="";
- if("M".equals(sex)){
- sexStr="男性";
- }else{
- sexStr="女性";
- }
- String weight=this.getWeight(sex, height);
- result.setText("你是一位:"+sexStr+"\n身高是:"+height+"厘米 \n你的标准体重:"+weight+"公斤");
- //返回上一页
- backButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Bundle2Activity.this.setResult(RESULT_OK, intent);
- Bundle2Activity.this.finish();
- }
- });
- }
- public String getWeight(String sex,double height){
- String weight="";
- if("M".endsWith(sex)){
- weight=format((height-80)*0.7);
- }else{
- weight=format((height-70)*0.6);
- }
- return weight;
- }
- public String format(double num){
- NumberFormat formatter=new DecimalFormat("0.00");
- String s=formatter.format(num);
- return s;
- }
- }
package com.cellcom; import java.text.DecimalFormat;
import java.text.NumberFormat; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class Bundle2Activity extends Activity{ private TextView result;
private Button backButton;
Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle2);
result=(TextView)findViewById(R.id.result);
backButton=(Button)findViewById(R.id.button_back); intent=this.getIntent();
Bundle bundle=intent.getExtras();
double height=bundle.getDouble("height");
String sex=bundle.getString("sex");
String sexStr="";
if("M".equals(sex)){
sexStr="男性";
}else{
sexStr="女性";
}
String weight=this.getWeight(sex, height);
result.setText("你是一位:"+sexStr+"\n身高是:"+height+"厘米 \n你的标准体重:"+weight+"公斤"); //返回上一页
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle2Activity.this.setResult(RESULT_OK, intent);
Bundle2Activity.this.finish();
}
});
} public String getWeight(String sex,double height){
String weight="";
if("M".endsWith(sex)){
weight=format((height-80)*0.7);
}else{
weight=format((height-70)*0.6);
}
return weight;
} public String format(double num){
NumberFormat formatter=new DecimalFormat("0.00");
String s=formatter.format(num);
return s;
}
}
5.复选框--CheckBox
- package com.cellcom;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- public class CheckBoxActivity extends Activity {
- private CheckBox plainCB;
- private CheckBox serifCB;
- private CheckBox boldCB;
- private CheckBox italicCB;
- private Button button1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.check_box);
- plainCB=(CheckBox)findViewById(R.id.plain_cb);
- serifCB=(CheckBox)findViewById(R.id.serif_cb);
- boldCB=(CheckBox)findViewById(R.id.bold_cb);
- italicCB=(CheckBox)findViewById(R.id.italic_cb);
- button1=(Button)findViewById(R.id.button1);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String result="";
- if(plainCB.isChecked()){
- result+=plainCB.getText()+"、";
- }
- if(serifCB.isChecked()){
- result+=serifCB.getText()+"、";
- }
- if(boldCB.isChecked()){
- result+=boldCB.getText()+"、";
- }
- if(italicCB.isChecked()){
- result+=italicCB.getText()+"、";
- }
- if(!"".equals(result)){
- result=result.substring(0,result.length()-1);
- }else{
- result="你没有选择!!!";
- }
- setTitle("Checked:"+result);
- }
- });
- }
- }
package com.cellcom; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox; public class CheckBoxActivity extends Activity { private CheckBox plainCB;
private CheckBox serifCB;
private CheckBox boldCB;
private CheckBox italicCB;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.check_box); plainCB=(CheckBox)findViewById(R.id.plain_cb);
serifCB=(CheckBox)findViewById(R.id.serif_cb);
boldCB=(CheckBox)findViewById(R.id.bold_cb);
italicCB=(CheckBox)findViewById(R.id.italic_cb);
button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result="";
if(plainCB.isChecked()){
result+=plainCB.getText()+"、";
}
if(serifCB.isChecked()){
result+=serifCB.getText()+"、";
}
if(boldCB.isChecked()){
result+=boldCB.getText()+"、";
}
if(italicCB.isChecked()){
result+=italicCB.getText()+"、";
}
if(!"".equals(result)){
result=result.substring(0,result.length()-1);
}else{
result="你没有选择!!!";
}
setTitle("Checked:"+result);
}
}); } }
6.菜单的实现--Menu
- package com.cellcom;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.widget.TextView;
- public class ContextMenuActivity extends Activity {
- private final static int ITEM0=Menu.FIRST;
- private final static int ITEM1=Menu.FIRST+1;
- private final static int ITEM2=Menu.FIRST+2;
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.context_menu);
- setTitle("单击Menu键看到效果!");
- tv=(TextView)findViewById(R.id.tv1);
- this.registerForContextMenu(tv);//将上下文注册到TextView
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- menu.add(0, ITEM0, 0, "红色背景");
- menu.add(0,ITEM1,0,"绿色背景");
- menu.add(0,ITEM2,0,"白色背景");
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case ITEM0:
- tv.setTextColor(Color.RED);
- break;
- case ITEM1:
- tv.setTextColor(Color.GREEN);
- break;
- case ITEM2:
- tv.setTextColor(Color.WHITE);
- break;
- default:
- break;
- }
- return true;
- }
- }
package com.cellcom; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView; public class ContextMenuActivity extends Activity { private final static int ITEM0=Menu.FIRST;
private final static int ITEM1=Menu.FIRST+1;
private final static int ITEM2=Menu.FIRST+2;
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.context_menu);
setTitle("单击Menu键看到效果!");
tv=(TextView)findViewById(R.id.tv1); this.registerForContextMenu(tv);//将上下文注册到TextView
} @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, ITEM0, 0, "红色背景");
menu.add(0,ITEM1,0,"绿色背景");
menu.add(0,ITEM2,0,"白色背景"); } @Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ITEM0:
tv.setTextColor(Color.RED);
break;
case ITEM1:
tv.setTextColor(Color.GREEN);
break;
case ITEM2:
tv.setTextColor(Color.WHITE);
break;
default:
break;
}
return true;
} }
7.日期选择器--DataTimePicker
- package com.cellcom;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.TimePicker;
- //DatePicker日期选择器、TimePicker时间选择器 使用
- public class DatePickerActivity extends Activity {
- private Button datePickerButton;
- private DatePicker datePicker1;
- private TimePicker timePicker1;
- //默认生成系统当前时间
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
- String str=sdf.format(new Date());
- int year=Integer.parseInt(str.substring(0, 4));
- int month=Integer.parseInt(str.substring(5,7))-1;
- int day=Integer.parseInt(str.substring(8,10));
- int hour=Integer.parseInt(str.substring(11,13));
- int minute=Integer.parseInt(str.substring(14,16));
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.date_picker);
- setTitle("DatePicker、TimePicker示例!");
- datePicker1=(DatePicker)findViewById(R.id.datePicker1);
- timePicker1=(TimePicker)findViewById(R.id.timePicker1);
- datePickerButton=(Button)findViewById(R.id.datePickerButton1);
- datePicker1.init(year,month,day, null);
- timePicker1.setCurrentHour(hour);
- timePicker1.setCurrentMinute(minute);
- timePicker1.setIs24HourView(true);
- datePickerButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- setTitle("你选择的是:"+datePicker1.getYear()+"年"+datePicker1.getMonth()+"月"+datePicker1.getDayOfMonth()+"日 "+timePicker1.getCurrentHour()+"小时"+timePicker1.getCurrentMinute()+"分");
- }
- });
- }
- }
package com.cellcom; import java.text.SimpleDateFormat;
import java.util.Date; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker; //DatePicker日期选择器、TimePicker时间选择器 使用
public class DatePickerActivity extends Activity { private Button datePickerButton;
private DatePicker datePicker1;
private TimePicker timePicker1; //默认生成系统当前时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String str=sdf.format(new Date());
int year=Integer.parseInt(str.substring(0, 4));
int month=Integer.parseInt(str.substring(5,7))-1;
int day=Integer.parseInt(str.substring(8,10));
int hour=Integer.parseInt(str.substring(11,13));
int minute=Integer.parseInt(str.substring(14,16)); @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.date_picker);
setTitle("DatePicker、TimePicker示例!"); datePicker1=(DatePicker)findViewById(R.id.datePicker1);
timePicker1=(TimePicker)findViewById(R.id.timePicker1);
datePickerButton=(Button)findViewById(R.id.datePickerButton1); datePicker1.init(year,month,day, null); timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute); timePicker1.setIs24HourView(true); datePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTitle("你选择的是:"+datePicker1.getYear()+"年"+datePicker1.getMonth()+"月"+datePicker1.getDayOfMonth()+"日 "+timePicker1.getCurrentHour()+"小时"+timePicker1.getCurrentMinute()+"分");
}
});
} }
8.网格视图控件--GridView
- package com.cellcom;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- //网络视图控件
- public class GridViewActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.grid_view);
- setTitle("网络视图控件GridView!");
- GridView gridView=(GridView)findViewById(R.id.grid_view);
- gridView.setAdapter(new ImageAdapter(this));
- }
- public class ImageAdapter extends BaseAdapter{
- private Context mContext;
- public ImageAdapter(Context context){
- mContext=context;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return mImageIds.length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if(convertView==null){
- imageView=new ImageView(mContext);
- imageView.setLayoutParams(new GridView.LayoutParams(85,85));
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- imageView.setPadding(8, 8, 8, 8);
- }else{
- imageView=(ImageView)convertView;
- }
- imageView.setImageResource(mImageIds[position]);
- return imageView;
- }
- }
- private Integer[] mImageIds = {
- R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
- R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
- R.drawable.sample_6, R.drawable.sample_7,R.drawable.sample_8,
- R.drawable.sample_9,R.drawable.sample_10,R.drawable.sample_11};
- }
package com.cellcom; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView; //网络视图控件
public class GridViewActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
setTitle("网络视图控件GridView!"); GridView gridView=(GridView)findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this)); } public class ImageAdapter extends BaseAdapter{ private Context mContext;
public ImageAdapter(Context context){
mContext=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}else{
imageView=(ImageView)convertView;
}
imageView.setImageResource(mImageIds[position]);
return imageView;
} } private Integer[] mImageIds = {
R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,R.drawable.sample_8,
R.drawable.sample_9,R.drawable.sample_10,R.drawable.sample_11}; }
9.进度条控件--ProgressBar
- package com.cellcom;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- /**
- *
- * @author nwang
- *
- * 进度条ProgressBar与线程使用。单击开始按钮,进度条水平方向前进!
- *
- */
- public class ProgressBarHandlerActivity extends Activity {
- /** Called when the activity is first created. */
- private Button startButton;
- private Button stopButton;
- private TextView result;
- private ProgressBar firstProgressBar;
- private final static int defaultValue=10;
- private int i=0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.progress_bar);
- setTitle("ProgressBar使用!");
- startButton=(Button)findViewById(R.id.startButton);
- stopButton=(Button)findViewById(R.id.stopButton);
- result=(TextView)findViewById(R.id.result);
- firstProgressBar=(ProgressBar)findViewById(R.id.firstProgressBar);
- firstProgressBar.setVisibility(0);//刚开始设置进度条可见
- startButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /*i+=defaultValue;
- firstProgressBar.setProgress(i);
- firstProgressBar.setSecondaryProgress(i+defaultValue);*/
- result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
- handler.post(progressBarThread);
- }
- });
- stopButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- handler.removeCallbacks(progressBarThread);
- }
- });
- }
- //创建Handler对象
- Handler handler=new Handler();
- //创建线程对象
- MyRunnable progressBarThread=new MyRunnable();
- class MyRunnable implements Runnable{
- @Override
- public void run() {
- i+=defaultValue;
- handler.postDelayed(progressBarThread, 1000);
- firstProgressBar.setProgress(i);
- firstProgressBar.setSecondaryProgress(i+defaultValue);
- result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
- if(i>=firstProgressBar.getMax()) i=0;
- }
- }
- }
package com.cellcom;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar; import android.widget.TextView; /**
*
* @author nwang
*
* 进度条ProgressBar与线程使用。单击开始按钮,进度条水平方向前进!
*
*/
public class ProgressBarHandlerActivity extends Activity {
/** Called when the activity is first created. */ private Button startButton;
private Button stopButton;
private TextView result;
private ProgressBar firstProgressBar;
private final static int defaultValue=10;
private int i=0; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_bar);
setTitle("ProgressBar使用!");
startButton=(Button)findViewById(R.id.startButton);
stopButton=(Button)findViewById(R.id.stopButton);
result=(TextView)findViewById(R.id.result);
firstProgressBar=(ProgressBar)findViewById(R.id.firstProgressBar); firstProgressBar.setVisibility(0);//刚开始设置进度条可见 startButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
/*i+=defaultValue;
firstProgressBar.setProgress(i);
firstProgressBar.setSecondaryProgress(i+defaultValue);*/
result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
handler.post(progressBarThread);
}
}); stopButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
handler.removeCallbacks(progressBarThread);
}
});
} //创建Handler对象
Handler handler=new Handler(); //创建线程对象
MyRunnable progressBarThread=new MyRunnable();
class MyRunnable implements Runnable{
@Override
public void run() {
i+=defaultValue;
handler.postDelayed(progressBarThread, 1000);
firstProgressBar.setProgress(i);
firstProgressBar.setSecondaryProgress(i+defaultValue);
result.setText(((double)i/firstProgressBar.getMax()*100)+"%");
if(i>=firstProgressBar.getMax()) i=0; } } }
10.单选控件--RadioGroup
- package com.cellcom;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.RadioButton;
- //单选控件
- public class RadioGroupActivity extends Activity {
- private RadioButton radioFemale;
- private RadioButton radioMale;
- private Button ok;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.radio_group);
- radioFemale=(RadioButton)findViewById(R.id.radioFemale);
- radioMale=(RadioButton)findViewById(R.id.radioMale);
- ok=(Button)findViewById(R.id.ok);
- ok.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String result="你选择的是:";
- if(radioFemale.isChecked()){
- setTitle(result+"女");
- }else{
- setTitle(result+"男");
- }
- }
- });
- }
- }
package com.cellcom; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton; //单选控件
public class RadioGroupActivity extends Activity { private RadioButton radioFemale;
private RadioButton radioMale;
private Button ok; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); setContentView(R.layout.radio_group); radioFemale=(RadioButton)findViewById(R.id.radioFemale);
radioMale=(RadioButton)findViewById(R.id.radioMale);
ok=(Button)findViewById(R.id.ok); ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result="你选择的是:";
if(radioFemale.isChecked()){
setTitle(result+"女");
}else{
setTitle(result+"男");
}
}
});
} }
11.打分组件--RatingBar
- package com.cellcom;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.RatingBar;
- import android.widget.Toast;
- //评分组件RatingBar、ImageView图片、 ImageButton图片按钮
- public class RatingBarActivity extends Activity {
- private RatingBar ratingBar;
- private ImageButton imageButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.rating_bar);
- setTitle("评分组件RatingBar、ImageView图片、 ImageButton图片按钮");
- ratingBar=(RatingBar)findViewById(R.id.rating_bar);
- imageButton=(ImageButton)findViewById(R.id.imageButton);
- imageButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(RatingBarActivity.this, "单击图片按钮!!", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
package com.cellcom; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Toast; //评分组件RatingBar、ImageView图片、 ImageButton图片按钮
public class RatingBarActivity extends Activity { private RatingBar ratingBar;
private ImageButton imageButton; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rating_bar);
setTitle("评分组件RatingBar、ImageView图片、 ImageButton图片按钮");
ratingBar=(RatingBar)findViewById(R.id.rating_bar);
imageButton=(ImageButton)findViewById(R.id.imageButton); imageButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(RatingBarActivity.this, "单击图片按钮!!", Toast.LENGTH_LONG).show();
}
});
}
}
12.标签控件--Tab
- package com.cellcom;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- //标签控件Tab
- public class TabDemoActivity extends TabActivity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle("标签控件Tab");
- TabHost tabHost=getTabHost();
- LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(),true);
- tabHost.addTab(tabHost.newTabSpec("biaoqian1").setIndicator("标签tab1").setContent(R.id.tab1));
- tabHost.addTab(tabHost.newTabSpec("biaoqian2").setIndicator("标签tab2").setContent(R.id.tab2));
- tabHost.addTab(tabHost.newTabSpec("biaoqian3").setIndicator("标签tab3").setContent(R.id.tab3));
- }
- }
package com.cellcom; import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost; //标签控件Tab
public class TabDemoActivity extends TabActivity { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("标签控件Tab");
TabHost tabHost=getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(),true);
tabHost.addTab(tabHost.newTabSpec("biaoqian1").setIndicator("标签tab1").setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("biaoqian2").setIndicator("标签tab2").setContent(R.id.tab2));
tabHost.addTab(tabHost.newTabSpec("biaoqian3").setIndicator("标签tab3").setContent(R.id.tab3));
}
}
13.子菜单的实现--SubMenu
- package com.cellcom;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.SubMenu;
- //子菜单subMenu
- public class SubMenuActivity extends Activity {
- private final static int ITEM_NEW_FILE=Menu.FIRST;
- private final static int ITEM_OPEN_FILE=Menu.FIRST+1;
- private final static int ITEM_COPY=Menu.FIRST+2;
- private final static int ITEM_CLOSE=Menu.FIRST+3;
- private final static int ITEM_SAVE=Menu.FIRST+4;
- private final static int ITEM_SAVE_ALL=Menu.FIRST+5;
- private final static int ITEM_CUT=Menu.FIRST+6;
- private final static int ITEM_PASTE=Menu.FIRST+7;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sub_menu);
- setTitle("单击Menu键看到效果!");
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- SubMenu file=menu.addSubMenu("文件");
- SubMenu edit=menu.addSubMenu("编辑");
- file.add(0,ITEM_NEW_FILE,0,"新建");
- file.add(0,ITEM_OPEN_FILE,0,"打开");
- file.add(0,ITEM_CLOSE,0,"关闭");
- file.add(0,ITEM_SAVE,0,"保存");
- file.add(0,ITEM_SAVE_ALL,0,"保存全部");
- edit.add(0,ITEM_COPY,0,"复制");
- edit.add(0,ITEM_CUT,0,"剪切");
- edit.add(0,ITEM_PASTE,0,"粘贴");
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case ITEM_NEW_FILE:
- setTitle("新建文件!");
- break;
- case ITEM_OPEN_FILE:
- setTitle("打开文件");
- break;
- case ITEM_COPY:
- setTitle("复制文件");
- break;
- default:
- break;
- }
- return true;
- }
- }
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所有控件的更多相关文章
-
Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
-
Android M 控件:Snackbar、Toolbar、TabLayout、NavigationView
Snackbar Snackbar提供了一个介于Toast和AlertDialog之间轻量级控件,它可以很方便的提供消息的提示和动作反馈.Snackbar的使用与Toast的使用基本相同: Snack ...
-
android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
-
Android基本控件之Menus
在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...
-
Android:控件布局(相对布局)RelativeLayout
RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...
-
矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
-
Android给控件添加触摸回调
Android给控件添加触摸回调 脑补一个场景,一个页面点击某个按钮会弹出PopupWindow,然后点击PopupWindow以外的任意位置关闭 效果图 实现方法 可以在布局的最外层容器监听触摸事件 ...
-
Android 基本控件相关知识整理
Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户.作为一个程序员如何才能开发出友好的图形界 ...
-
Github上star数超1000的Android列表控件
Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...
随机推荐
-
java之并发
一.线程 在java中多线程执行任务,主要有两种方式,一种是通过继承Thread类,重写run方法,优点是比较方便的创建一个线程,缺点是java中每个类只能有一个基类,所有继承了T火热ad类后,就不能 ...
-
Mysql 数据库文件存储在哪个目录
也就是说我在mysql里建了一个叫 ac 的数据库,但是我找不到其存储位置,Mysql里面的数据库是怎么存储的,是否也像sqlserver 那样,有一个日志文件和数据文件? mysql数据库在系统上是 ...
-
VI 命令学习指南
ctrl+b 屏幕向后(上)移动一屏 ctrl+f 屏幕向前(下)移动一屏 ctrl+u 屏幕向后(上)移动半屏 ctrl+d 屏幕向前(下)移动半屏
-
MySQL 数据库操作命令汇总
此文全部都是基本的数据库语言 1.登陆到mysql >mysql -h hostname -u username -p 然后等待系统提示输入密码即可登陆.如果想在登陆的时候就选择好数据库,可以使 ...
-
iOS开发之让你的应用“动”起来
概览在 iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互 式绘图,如何通过核心动画创建基础动画.关键帧动 ...
-
GetFontResourceInfo 函数: 获取字体文件对应的字体名
在 win 7, gdi32.dll,找不到 GetFontResourceInfo(), 但能找到 GetFontResourceInfoW(), 用法类似. //----------------- ...
-
webpack4学习笔记
执行webpack-dev-server的时候不会自动生成dist打包目录,怪不得公司的项目里面都没用到webpack-dev-server呢 执行webpack的时候会生成dist目录 watch的 ...
-
阿里云mysql安装配置(CentOS 7.3 64)
自建目录并且加载yum资源mysql 安装 回车之后竟然出现不可以的情况(原因是原来的镜像里面默认装好了mysql5.7) 然后只能尝试跳过密码登录 #vim /etc/my.cnf 在文档内搜索my ...
-
Bugku-CTF之flag.php(点了login咋没反应)
Day20 flag.php 地址:http://123.206.87.240:8002/flagphp/ 点了login咋没反应 提示:hint
-
nginx实践(四)之静态资源web服务(防盗链)
防盗链目的 防止资源被盗用 http_refer 主要是判断refer信息,判断请求来源是不是合法身份 语法 实例 参数说明: none表示允许没有代理的头信息过来,blocked表示refer信息不 ...