您好,登錄后才能下訂單哦!
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);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。