使用onCreateDialog建立Dialog,並且動態修改Dialog的資訊
阿新 • • 發佈:2019-01-22
在開發檔案管理器的過程中遇到過這樣的問題,因為檔案管理器中同一級的所有檔案的名字都是不同的,所以在對不同檔案操作時希望可以提供給使用者一些資訊,讓使用者瞭解當前正在操作的檔案是哪個檔案。例如,使用者想要對檔案進行重新命名,那我首先彈出一個Dialog,然後Dialog的標題是正在操作的檔名字,Dialog中提供一個EditText供使用者輸入新的名字。可是在測試過程中出現了這樣的事情,當操作第一個檔案時,顯示的資訊是正確的,然而當操作第二個檔案時,顯示的標題還是第一個檔案的名字,之後無論你操作哪個檔案,顯示的都是第一個檔案的名字。上網查了一些資料,解釋說如果你使用onCreateDialog方法建立對話方塊,而又想動態地更新它顯示的資訊,那你必須重寫onPrepareDialog方法,在這個方法中對Dialog的顯示資訊進行設定,並且你在onCreateDialog方法中對這些顯示資訊的設定不可以為空,否則在onPrepareDialog對這些顯示資訊進行重新設定不會生效。下面是一個簡單的例子,僅供參考:
main.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/showinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/hello" />
</LinearLayout>
Dialog中使用的contentView佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="新名字"
android:textSize="20sp" />
<EditText
android:id="@+id/new_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
主程式
package com.ygc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class DialogTestActivity extends Activity {
/** Called when the activity is first created. */
// 載入重新命名輸入框佈局檔案
private LayoutInflater factory;
// 重新命名檢視
private View renameView;
// 顯示文字
private TextView showInfo;
// 使用者輸入新名字的輸入框
private EditText input;
// 上下文資訊
private Context mContext;
// 顯示Dialog的ID
private static final int SHOW_RENAME_DIALOG = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
initUI();
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch (id) {
case SHOW_RENAME_DIALOG:
dialog = new AlertDialog.Builder(mContext)
.setTitle(showInfo.getText())
.setView(renameView)
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
showInfo.setText(input.getText());
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).create();
break;
default:
dialog = null;
}
return dialog;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
switch (id) {
case SHOW_RENAME_DIALOG:
dialog.setTitle(showInfo.getText());
break;
}
super.onPrepareDialog(id, dialog, args);
}
/**
* 初始化控制元件
* */
private void initUI() {
factory = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
renameView = factory.inflate(R.layout.rename_layout, null);
input = (EditText) renameView.findViewById(R.id.new_name);
showInfo = (TextView) findViewById(R.id.showinfo);
showInfo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(SHOW_RENAME_DIALOG);
}
});
}
}
注意,建立Dialog使用的mContext必須是Activity的context而不能使用getApplicationContext(),否則會報如下錯誤: