当前位置: 代码迷 >> Android >> android一些核心效能程序代码
  详细解决方案

android一些核心效能程序代码

热度:27   发布时间:2016-05-01 13:45:31.0
android一些核心功能程序代码

1、对话框:

@Override
?protected Dialog onCreateDialog(int id) {
??switch(id){
??case DIALOG_WELCOME:
???return new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_info).setTitle("欢迎").setMessage("欢迎使用本程序!").setPositiveButton("确定", new OnClickListener(){

????public void onClick(DialogInterface arg0, int arg1) {
?????// TODO Auto-generated method stub
????}
???}).create();
??default: return null;
??}
?}??

?

2、上下文菜单事件:

@Override
?public boolean onContextItemSelected(MenuItem item) {
??super.onContextItemSelected(item);
??switch(item.getItemId()){
??case C_MENU_NEW:
???//
???showDialog(DIALOG_WELCOME);
???break;
??case C_MENU_OPEN:
???//
???break;
??}
??return false;
?}

3、选项菜单事件

@Override
?public boolean onOptionsItemSelected(MenuItem item) {
??super.onOptionsItemSelected(item);
??switch(item.getItemId()){
??case MENU_SAVE:
???//保存操作
???
???break;
??case MENU_DELETE:
???//删除操作
???break;
??}
??return false;
?}

4、创建上下文菜单

@Override
?public void onCreateContextMenu(ContextMenu menu, View v,
???ContextMenuInfo menuInfo) {
??super.onCreateContextMenu(menu, v, menuInfo);
??menu.add(0,C_MENU_NEW,0,"新建");
??menu.add(0,C_MENU_OPEN,0,"打开");
?}

5、创建选项菜单

@Override
?public boolean onCreateOptionsMenu(Menu menu) {
??//menu.add(0, MENU_SAVE, 0, "保存").setIcon(android.R.drawable.ic_menu_save);
??//super.onCreateOptionsMenu(menu);
??SubMenu sub=menu.addSubMenu("搜索");
??sub.setIcon(android.R.drawable.ic_menu_search);
??sub.add(0, MENU_LOCAL, 0, "本地");
??sub.add(0,MENU_INTERNET,0,"网络");
??//menu.add(0, MENU_DELETE, 0, "删除").setIcon(android.R.drawable.ic_menu_delete);
??return true;
?}

1、时间选择器
private int DIALOG_DATEPICKER=1;
private Calendar c;
@Override
?protected Dialog onCreateDialog(int id) {
??switch(id){
??case DIALOG_DATEPICKER:
???return new DatePickerDialog(this,new OnDateSetListener(){

????public void onDateSet(DatePicker arg0, int arg1, int arg2,
??????int arg3) {
?????/**
????? *? 这是设置好日期后的回调方法,
????? *? 可以根据设置的内容更新用户界面,
????? *? 或者系统时间
????? */
????}
????},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
??default:
????return null;
??}
?}

?

2、进度条
final int MSG_PROGRESS=1;
?final int MAXPROGRESS=100;
?final int DIALOG_PROGRESS=1;
?ProgressDialog pd;
?Handler pHandler;
[email protected]
?protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.main);
??pHandler=new Handler(){

[email protected]
???public void handleMessage(Message msg) {
????super.handleMessage(msg);
????switch(msg.what){
????case MSG_PROGRESS:
?????if(pd.getProgress()>=MAXPROGRESS){
??????pd.dismiss();
?????}else{
??????pd.incrementProgressBy(1);
??????pHandler.sendEmptyMessageDelayed(MSG_PROGRESS, 100);
?????}
?????break;
?????default:
??????break;
????}
???}
???
??};
??this.showDialog(DIALOG_PROGRESS);
??pHandler.sendEmptyMessage(MSG_PROGRESS);
?}
[email protected]
?protected Dialog onCreateDialog(int id) {
??switch(id){
??case DIALOG_PROGRESS:
???pd=new ProgressDialog(this);
???pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
???pd.setMax(MAXPROGRESS);
???pd.setProgress(0);
???return pd;
??default:
????return null;
??}
?}

3、数据库绑定
public class Test extends ListActivity {
?String[] weekStrings=new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
?ArrayAdapter<String> adapter;
[email protected]
?protected void onCreate(Bundle savedInstanceState) {
??adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,weekStrings);
??this.setListAdapter(adapter);
?}
?
}

1、自己做的拨号机
public class TinyDialer extends Activity {
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? final EditText phoneNumber=(EditText)this.findViewById(R.id.phonenumber_id);
??????? final Button button=(Button)this.findViewById(R.id.button_id);
??????? button.setOnClickListener(new Button.OnClickListener(){

???public void onClick(View arg0) {
????// TODO Auto-generated method stub
????String callee=phoneNumber.getText().toString();
????if(PhoneNumberUtils.isGlobalPhoneNumber(callee)){
?????Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel://"+callee));
?????startActivity(i);
????}else{
?????Toast.makeText(TinyDialer.this,R.string.notify_incorrect_phonenumber,Toast.LENGTH_LONG).show();
????}
???}
??????? });
??? }
}

2、发送短信程序
public class TinySMS extends Activity {
?private EditText txtphoneNo;
?private EditText txtMessage;
?Button btnSendSMS;
[email protected]
?protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??txtphoneNo=this.findViewById(R.layout.txtPhoneNo);
??txtMessage=this.findViewById(R.layout.txtMessage);
??btnSendSMS=this.findViewById(R.layout.btnSendSMS);
??btnSendSMS.setOnClickListener(new OnClickListener(){
???public void onClick(View arg0) {
????String phoneNo=txtphoneNo.getText().toString();
????String message=txtMessage.getText().toString();
????if(phoneNo.length()>0&&message.length()>0){
?????sendSMS(phoneNo,message);
????}else{
?????Toast.makeText(TinySMS.this,"请输入电话号码和短信内容!",Toast.LENGTH_LONG).show();
????}
???}
??});
?}
?private void sendSMS(String phoneNumber,String message){
??PendingIntent pi=PendingIntent.getActivity(this,0,new Intent(this,TinySMS.class),0);
??SmsManager sms=SmsManager.getDefault();
??sms.sendTextMessage(phoneNumber, null, message, pi, null);
?}
?
}
注意:androidManifest.xml中声明权限:<user-pemission android:name="android.permission.SEND_SMS" />

1 楼 sword_java 2012-03-15  
onCteateDialog 有什么用? 直接 diaolog.show() 不就行了么?
  相关解决方案