編輯:關於Android編程
package com.example.dialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button simpleButton,listButton,singleButton,multiButton,customButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 simpleButton = (Button) findViewById(R.id.info_dg); listButton = (Button) findViewById(R.id.list_dg); singleButton = (Button) findViewById(R.id.single_dg); multiButton = (Button) findViewById(R.id.multi_dg); customButton = (Button) findViewById(R.id.custom_dg); //設置監聽器 simpleButton.setOnClickListener(this); listButton.setOnClickListener(this); singleButton.setOnClickListener(this); multiButton.setOnClickListener(this); customButton.setOnClickListener(this); } /* * 彈出單選型的彈框 * */ public void showMultiDialog(){ //聲明並初始化數據 final CharSequence[] items = {"籃球","足球","游泳","唱歌"}; final StringBuilder checkedStr = new StringBuilder(); //聲明並初始化AlertDialog.Builder AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置AlertDialog標題 builder.setTitle("多選彈框標題"); //設置圖標 builder.setIcon(R.drawable.a12); //設置數據 builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked){ checkedStr.append(items[which].toString()); } } }); builder.setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,checkedStr,Toast.LENGTH_SHORT).show(); } }); //展示彈框 builder.show(); } /* * 彈出單選型的彈框 * */ public void showSingleDialog(){ //聲明並初始化數據 final CharSequence[] items = {"男","女","不男不女"}; //聲明並初始化AlertDialog.Builder AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置AlertDialog標題 builder.setTitle("單選彈框標題"); //設置圖標 builder.setIcon(R.drawable.a12); //設置數據 builder.setSingleChoiceItems(items,1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String selectItem = items[which].toString(); Toast.makeText(MainActivity.this,selectItem,Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"確認",Toast.LENGTH_SHORT).show(); } }); //展示彈框 builder.show(); } /* * 彈出list型的彈框 * */ public void showListDialog(){ //聲明並初始化數據 final CharSequence[] items = new CharSequence[5]; for (int i =0;i<5;i++){ items[i] ="數據"+i; } //聲明並初始化AlertDialog.Builder AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置AlertDialog標題 builder.setTitle("List型彈框標題"); //設置圖標 builder.setIcon(R.drawable.a12); //設置數據並實現點擊監聽 builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //獲取選中項文本 String selectItem = items[which].toString(); Toast.makeText(MainActivity.this,selectItem,Toast.LENGTH_SHORT).show(); } }); builder.show(); } /* * 彈出簡單的彈框 * */ public void showSimpleDialog(){ //聲明並初始化AlertDialog.Builder AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置AlertDialog標題 builder.setTitle("普通彈框標題"); //設置圖標 builder.setIcon(R.drawable.a12); //設置提示信息 builder.setMessage("普通彈框提示信息..."); //設置確認按鈕 builder.setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"確認",Toast.LENGTH_SHORT).show(); } }); //設置普通按鈕 builder.setNeutralButton("快點我", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"我就知道你要點我",Toast.LENGTH_SHORT).show(); } }); //設置取消按鈕 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show(); } }); builder.show(); } /* * 彈出自定義的彈框 * */ public void showCustomDialog(){ final EditText userName,passWord; //聲明並初始化布局加載器 //LayoutInflater Inflater =getLayoutInflater(); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); //加載自定義布局 View view = inflater.inflate(R.layout.custom_dialog,null); //初始化布局控件 userName = (EditText) view.findViewById(R.id.username_et); passWord = (EditText) view.findViewById(R.id.pas_et); //聲明並初始化AlertDialog.Builder AlertDialog.Builder builder = new AlertDialog.Builder(this); //設置布局 builder.setView(view); //設置登錄按鈕 builder.setPositiveButton("登陸", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String nameStr = userName.getText().toString(); String passStr = passWord.getText().toString(); Toast.makeText(MainActivity.this,"用戶名:"+nameStr+",密碼:"+passStr,Toast.LENGTH_SHORT).show(); } }); //設置取消按鈕 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"取消登錄",Toast.LENGTH_SHORT).show(); } }); builder.show(); } /* * 繼承實現監聽響應 * */ @Override public void onClick(View v) { switch (v.getId()){ case R.id.info_dg: showSimpleDialog(); break; case R.id.list_dg: showListDialog(); break; case R.id.single_dg: showSingleDialog(); break; case R.id.multi_dg: showMultiDialog(); break; case R.id.custom_dg: showCustomDialog(); break; } } }
示例一:實現通知欄管理當針對相同類型的事件多次發出通知,作為開發者,應該避免使用全新的通知,這時就應該考慮更新之前通知欄的一些值來達到提醒用戶的目的。例如我們手機的短信系
概述:一般情況下,我們知道View類有個View.OnTouchListener內部接口,通過重寫他的onTouch(View v, MotionEvent event)
直接上代碼: private void setDialog(){ View view = getLayoutInflater().inflate(R.
通知(Notification)是android系統中比較有特色的一個功能,當某個應用程序希望向用戶發出一些提示信息,而該應用程序又不在前台運行時,就可以借助通知來實現。