2010-11-06 96 views
2

我有一個Android自定義對話框的小問題。在自定義對話框中處理按鈕

我建立一個自定義對話框中的onCreateDialog(int)的函數:

dialog = new Dialog(this); 
dialog.setContentView(R.layout.custom_dialog); 
dialog.setTitle("Custom Dialog"); 

我在同一個類中的onClick(視圖)功能:

public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.dialog_button: 
      Log.i("pma57","dialog button pressed"); 
      break; 
     case R.id.main_button: 
      showDialog(DIALOG_CUSTOM); 
      break; 
     }  
    } 

這是XML定義:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:paddingBottom="20dp"> 
     <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/enter_username" /> 
     <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
     <Button 
     android:id="@+id/dialog_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="OK" 
     android:onClick="onClick" /> 

</LinearLayout> 

對話框出現。但按鈕不起作用(應用程序崩潰) - 這很好,因爲回調的onClick函數是在我的主要活動中定義的 - 而且對話框是一個新的活動(對嗎?)。

但我真的不知道我是如何在對話框中實現一個按鈕 - 我認爲這是一個技術的基本理解問題。很長的路要將Dialog的子類化,然後把所有的東西都寫出來 - 但還有另一種我沒有看到的方式嗎?

+0

我在您的xml文件中看不到main_button。這可能是你遇到的問題。 – prolink007 2010-11-06 16:25:26

+0

這是因爲它只是程序的重要組成部分 - 主要按鈕在主要活動的另一個xml文件中。 – Herrbert 2010-11-06 21:22:14

回答

21

繞着它的方式,我用的,而不是一個開關塊是使用onClickListeners的按鈕:

dialog = new Dialog(this); 
dialog.setContentView(R.layout.custom_dialog); 
dialog.setTitle("Custom Dialog"); 


Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button); 
dialog_btn.setOnClickListener(new View.OnClickListener() 
{ 
    // Perform button logic 
} 

請注意,您從對話框中找到視圖,而不是直接調用findViewById,因爲它將返回空指針,因爲應用程序視圖上不會有dialog_button。

+0

ahhh。我嘗試了這種方式,但我錯過了dialog.findViewById()之前的對話框,這可能是我的正確解決方案!謝謝! – Herrbert 2010-11-06 21:21:18

+0

是的,我試了它 - 與dialog.findViewById()我能夠設置偵聽器到我的主要活動 - 現在就像一個魅力。再次感謝你! – Herrbert 2010-11-06 21:29:55

+0

沒問題 - 最簡單的方法來記住它,當你在你的活動中工作時,它會有一個視圖 - 如果你調用findViewById,你會得到對視圖中對象(按鈕)ID的引用 - 這裏的上下文是你的活動 - 如果您創建第二個視圖 - 在這種情況下,您想要dialog.findViewById的對話框(可以明顯命名爲任何對象),因爲這將從查看​​用於引用的對話視圖的參考的上下文視圖改變。 – Scoobler 2010-11-06 22:32:12

-2

我必須說我真的不知道你在做什麼。要顯示一個對話框,您可以使用AlertDialog類,並添加你通過代碼所需要的按鈕:

private void insertSelfPhonenumberRequestDialog() { 
    final View layout = View.inflate(this, R.layout.dialog_phonenumber_request, null); 
    ((TextView) layout.findViewById(R.id.label)).setText(getString(R.string.dialog_insert_self_phonenumber_request_text1)); 
    ((TextView) layout.findViewById(R.id.hint)).setText(getString(R.string.dialog_general_info_text1)); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setIcon(0); 
    builder.setTitle(getString(R.string.dialog_insert_self_phonenumber_request_title)); 

    builder.setPositiveButton(getString(R.string.save), new Dialog.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //whatever 
     } 
    }); 
    builder.setView(layout); 
    builder.show(); 
} 
+0

是的,我可以使用一個警告對話框 - 但這是我聽到的演講的任務 - 我必須使用自定義對話框。 – Herrbert 2010-11-06 21:20:12

相關問題