2011年5月19日 星期四

嵌入自訂內容的 Dialog

簡單的作法可以參考 [Android] AlertDialog-嵌入客製化表單
在 AlertDialog 中加入 EditText,不過上面沒有取得 EditText 內容的方法
下面是我的 code:


public class customizeDialog extends Activity {
  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.addPreferencesFromResource(R.xml.app_settings);
    Preference prefBackup = this.findPreference(this.Pref_Backup);
    prefBackup.setOnPreferenceClickListener(this.onPrefClick);
  }
  
  //--------------------------------------------------------//
  //------------------------ events ------------------------//
  //--------------------------------------------------------//
  
  public OnPreferenceClickListener onPrefClick = new OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference pref) {
      // TODO Auto-generated method stub
      if(pref.getKey().compareTo("dialogWithEditText") == 0)
        showDialog(R.id.dialogEdit);
      return false;
    }
  };
  
  public Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    switch (id) {
    case R.id.dialogEdit:
      AlertDialog.Builder builder = new AlertDialog.Builder(AppSettings.this);
      // 取得 View
      LayoutInflater inf = LayoutInflater.from(AppSettings.this);
      View linear = inf.inflate(R.layout.dialog_with_edittext, null);
      builder.setView(linear);
      
      // 設定輸入框
      final EditText edit = (EditText) linear.findViewById(R.id.dialogEdit);
      builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
          // TODO Auto-generated method stub
          Toast.makeText(customizeDialog.this, "輸入" + edit.getText(), Toast.LENGTH_SHORT).show();
        }
      });
      builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
          // TODO Auto-generated method stub
          Toast.makeText(customizeDialog.this, "按下取消按鈕", Toast.LENGTH_SHORT).show();
        }
      });
      builder.setTitle("對話框標題");
      builder.setMessage("對話框文字");
      dialog = builder.create();
      break;
    }
    return dialog;
  }
}

這是用了 onCreateDialog( ) 跟 showDialog( ) 函式的顯示對話框的方式。
順道提一下,原本沒有用這個的時候一直出現奇怪的錯誤訊息 XD
像是 WindowsManager ..... 什麼的 Exception~
記一下原本一直出錯的程式碼:

public class customizeDialog extends Activity {
  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.addPreferencesFromResource(R.xml.app_settings);
    Preference prefBackup = this.findPreference(this.Pref_Backup);
    prefBackup.setOnPreferenceClickListener(this.onPrefClick);
  }
  
  //--------------------------------------------------------//
  //------------------------ events ------------------------//
  //--------------------------------------------------------//
  
  public OnPreferenceClickListener onPrefClick = new OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference pref) {
      // TODO Auto-generated method stub
      if(pref.getKey().compareTo("dialogWithEditText") == 0) {
        AlertDialog.Builder builder = new AlertDialog.Builder(AppSettings.this);
        // 取得 View
        LayoutInflater inf = LayoutInflater.from(AppSettings.this);
        View linear = inf.inflate(R.layout.dialog_with_edittext, null);
        builder.setView(linear);
      
        // 設定輸入框
        final EditText edit = (EditText) linear.findViewById(R.id.dialogEdit);
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(customizeDialog.this, "輸入" + edit.getText(), Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(customizeDialog.this, "按下取消按鈕", Toast.LENGTH_SHORT).show();
          }
        });
        builder.setTitle("對話框標題");
        builder.setMessage("對話框文字");
        dialog = builder.create();
      }
    }
  }
}

兩種寫法的差別就是上面的是把建立 Dialog 的語法寫在 onCreateDialog 裡
下面則是直接寫在 onPreferenceClick 裡(會出現錯誤)

沒有留言:

張貼留言