2025-04-22

安卓Dialog彈出對話框全解:包含瞭AlertDialog,DialogFragment。Dialog基類中並沒有定義界面,所以如果使用dialog類設置彈出框,需要使用xml自定義UI。

當然系統也自帶瞭幾個dialog派生的彈出框,例如Alert Dialog,並設置好瞭UI,你可以直接調用系統自帶的dialog衍生品。除此之外,activity和fragment也可以作為dialog樣式彈出使用。

dialog基類創建彈出框

1、首先你需要為你的dialog設置佈局界面

這裡添加佈局界面為dialog_view.xml文件



  

2、使用Dialog類創建一個新的對話框

    private void displayNewDialog() {
      Dialog dialog = new Dialog(Dialog_Activity.this);  //為當前窗口創建一個彈出框
      //設置標題
      dialog.setTitle("Dialog Title");
      //填充佈局
      dialog.setContentView(R.layout.dialog_view);

      //更新對話框的UI
      TextView text = (TextView)dialog.findViewById(R.id.dialog_text_view);
      text.setText("This is the text in my dialog");

      //顯示對話框
      dialog.show();
    }

使用Alert Dialog定義彈出框

 //配置一個Alert Dialog
    private void displayAlertDialog() {
      Context context = Dialog_Activity.this;
      String title = "對話框標題";
      String message = "對話框內容";
      String button1String = "確定";
      String button2String = "取消";

      AlertDialog.Builder ad = new AlertDialog.Builder(context);
      ad.setTitle(title);
      ad.setMessage(message);

      //確定按鈕點擊事件
      ad.setPositiveButton(button1String,new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int arg1) {
            Log.v("彈出框", "點擊瞭確定按鈕");
          }
        }
      );

      //取消按鈕點擊事件
      ad.setNegativeButton(button2String,new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface dialog, int arg1) {
              Log.v("彈出框", "點擊瞭取消按鈕");
          }
        }
      );

      //設置對話框在點擊返回物理鍵時的操作。
      ad.setCancelable(true);
      ad.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            Log.v("彈出框", "點擊瞭物理鍵——返回");
        }
      });

      //顯示對話框
      ad.show();
    }

將fragment定義為dialog顯示

系統自帶瞭DialogFragment類用來實現使用fragment實現dialog效果,DialogFragment繼承自Fragment。

dialog的界面還是使用上面的dialog_view.xml文件

下面實現一個自定義的DialogFragment。其中設置佈局有兩種方式,可以在onCreateView函數中返回一個自定義佈局的view,也可以在onCreateDialog函數中函數一個dialog。不過兩者不能同時使用。

import com.lp.app.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class DialogFragment1 extends DialogFragment {


  public static DialogFragment1 newInstance(String title,String message) {
    //創建一個新的帶有指定參數的Fragment實例
    DialogFragment1 fragment = new DialogFragment1();
    Bundle args = new Bundle();
    args.putString("title", title);   //將參數存儲在Bundle,在程序暫停重啟不會被銷毀
    args.putString("message", message);   //將參數存儲在Bundle,在程序暫停重啟不會被銷毀
    fragment.setArguments(args);

    return fragment;
  }

//使用onCreatDialog事件處理程序。onCreateView與onCreatDialog隻能二選一
//  @Override
//  public Dialog onCreateDialog(Bundle savedInstanceState) {
//          //獲取參數
//          String title = getArguments().getString("title");
//          String message = getArguments().getString("message");
//          //使用AlertBuilder創建新的對話框
//          AlertDialog.Builder timeDialog = new AlertDialog.Builder(getActivity());
//          //配置對話框UI.
//          timeDialog.setTitle(title);
//          timeDialog.setMessage(message);
//          //返回配置完成的對話框
//          return timeDialog.create();
//  }
//  
  //使用onCreateView自定義佈局。onCreateView與onCreatDialog隻能二選一

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
      //獲取參數
      String title = getArguments().getString("title");
      String message = getArguments().getString("message");
      //填充對話框的UI
      View view = inflater.inflate(R.layout.dialog_view, container, false);

      //更新對話框內容
      TextView textview = (TextView)view.findViewById(R.id.dialog_text_view);
      textview.setText(message);

      return view;
  }


}

再在需要彈出dialog的地方,彈出這個對話框

    private void showDialogFragment() {
      String title = "標題";
      String message = "要顯示的文本";
      String tag = "my_dialog";
      DialogFragment myFragment = DialogFragment1.newInstance(title,message);
      myFragment.show(getFragmentManager(), tag);
    }

將activity定義為dialog顯示

隻需要在mainfest文件中註冊activity時設置

android:theme="@android:style/theme.Dialog"

可以使窗口表現如對話框

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *