您好,登錄后才能下訂單哦!
封裝的Dialog對話框,有時候需要獲取選項這時候就用到了函數的回調
package com.example.testlistq; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.KeyEvent; public class MyDialog { private static AlertDialog dialog; //回調接口 public interface ResultCallBack{ public void callback(boolean flag); } public static void showDialog(Context context, String str, final ResultCallBack mRCallBack){ dialog = new AlertDialog.Builder(context) .setTitle("提示") .setMessage(str) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(mRCallBack != null){ mRCallBack.callback(true); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(mRCallBack != null){ mRCallBack.callback(false); } } }).create(); dialog.setCancelable(false); dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_SEARCH) {//屏蔽搜索鍵 return true; } else { return false; //默認返回 false } } }); dialog.show(); } }
//調用實例
MyDialog.showDialog(this, "回調窗口", new ResultCallBack() { @Override public void callback(boolean flag) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "flag = " + flag, Toast.LENGTH_SHORT).show(); } });
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。