整理的Android多种控件步骤

Notification步骤:

1、onCreate方法中mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2、在点击事件事件监听中:

  Notification notification=new Notification(R.drawable.earlybird, "小鸟飞 啊飞", System.currentTimeMillis());

  Intent intent=new Intent(MainActivity.this,SecondActivity.class);

  PendingIntent contentIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

  notification.setLatestEventInfo(MainActivity.this, "愤怒的小鸡", "愤怒的小鸟与愤怒的小鸡", contentIntent);

  notification.flags=Notification.FLAG_AUTO_CANCEL;//点击后自动消失

  mNotificationManager.notify(1, notification);




Toast步骤:

   1、Toast.makeText(MainActivity.this, "我是愤怒的小鸡", Toast.LENGTH_SHORT).show();

   //切记记得.show()方法,让Toast显示出来

  //Toast不会获取焦点,提示消息过了一段时间会自动消失,不需要用户去点击确认,也不需要用户去反馈

  //在应用的其他界面依然能够看到Toast提示




Dialog步骤:

第一种方法:

1、创建创建AlertDialog.Builder对象  

  AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

2 、设置对话框标题,内容,图标

   builder.setTitle("退出");

   builder.setIcon(R.drawable.earlybird);

   builder.setMessage("是否真的退出?");

3、设置对话框的按钮

  builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();

}

});

   builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "取消删除", Toast.LENGTH_LONG).show();

}

});

4、通过AlertDialog.Builde对象创建AlertDialog对象

  AlertDialog dialog = builder.create();

  dialog.show();

第二种方法:

1、在点击监听事件中showDialog(1);

2、重写OnCreateDialog方法

  protected Dialog onCreateDialog(int id) {

switch (id) {

case 1:

dialog=dialog_1();

break;


default:

break;

}

return dialog;

}

3、重写dialog_1()方法:

  private AlertDialog dialog_1() {

AlertDialog.Builder builder = new AlertDialog.Builder(

MainActivity.this);

builder.setTitle("退出");

builder.setIcon(R.drawable.earlybird);

builder.setMessage("是否真的退出?");

builder.setPositiveButton("确定",

new DialogInterface.OnClickListener() {


@Override

public void onClick(DialogInterface dialog,

int which) {

Toast.makeText(MainActivity.this, "删除成功",

Toast.LENGTH_LONG).show();


}

});

builder.setNegativeButton("取消",

new DialogInterface.OnClickListener() {


@Override

public void onClick(DialogInterface dialog,

int which) {

Toast.makeText(MainActivity.this, "取消删除",

Toast.LENGTH_LONG).show();


}

});

return builder.create();

}




optionMenu步骤:

第一种方法:

1、在onCreateOptionsMenu方法中

  public boolean onCreateOptionsMenu(Menu menu) {

menu.add(0, 1, 0, "设置");

menu.add(0, 2, 0, "帮助");

menu.add(0, 3, 0, "添加");

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

2、重写onOptionsItemSelected方法

  public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case 1:

Toast.makeText(this, "成功进入设置", Toast.LENGTH_LONG).show();

break;

case 2:

Toast.makeText(this, "成功进入帮助", Toast.LENGTH_LONG).show();

break;

case 3:

Toast.makeText(this, "成功进入添加", Toast.LENGTH_LONG).show();

break;


default:

break;

}

return super.onOptionsItemSelected(item);

}

第二种方法:

1、在menu文件夹中创建xml文件

2、重写重写onOptionsItemSelected方法

  public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.item1:

Toast.makeText(this, "呵呵", Toast.LENGTH_LONG).show();

break;

case R.id.item2:

Toast.makeText(this, "哈哈", Toast.LENGTH_LONG).show();


default:

break;

}

return super.onOptionsItemSelected(item);

}




ContextMenu步骤:

1、在onCreate方法中

  tv = (TextView) findViewById(R.id.textView1);

  registerForContextMenu(tv);

2、重写onCreateContextMenu方法

  public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

menu.add(0,1,0,"复制");

menu.add(0, 2, 0, "编辑");

super.onCreateContextMenu(menu, v, menuInfo);

}

3、重写onContextItemSelected方法

   public boolean onContextItemSelected(MenuItem item) {

switch (item.getItemId()) {

case 1:

Toast.makeText(this, "复制成功", Toast.LENGTH_LONG).show();

break;

case 2:

Toast.makeText(this, "粘贴成功", Toast.LENGTH_LONG).show();

default:

break;

}

return super.onContextItemSelected(item);

   }



本文出自 “鹭岛猥琐男” 博客,请务必保留此出处http://20110318.blog.51cto.com/8651374/1365984

整理的Android多种控件步骤,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。