2011-04-19 93 views
19

我在關閉提醒對話框時遇到問題。我正在使用佈局充氣器來製作對話框,因此我不確定在完成該操作後如何關閉它。代碼如下:關閉按鈕上的自定義提醒對話框單擊

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this); 
DialogInterface dia = new DialogInterface(); 

//Create a custom layout for the dialog box 
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false); 

TextView title = (TextView)layout.findViewById(R.id.rep_1_title); 
Button add_item = (Button)layout.findViewById(R.id.add_button); 

add_item.setOnClickListener(new OnClickListener() 
{ 
     @Override 
     public void onClick(View v) 
     { 
     //Need this to close the alert dialog box 
     } 
}); 

title.setText(workout_items[position]); 
dialog.setView(layout); 
dialog.show(); 

我不能稱之爲完成,因爲關閉該警告對話框中,從啓動列表視圖,以及dialog.dismiss電話不提供給我。

你認爲我應該怎麼做才能解決這個問題?

+4

調用'解僱( );' – Vivek 2011-04-19 08:09:56

回答

0

嘗試dialog.cancel();在你的onclick監聽器中。 它應該工作。

16

像這樣做,

add_item.setOnClickListener(new OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
         dialog.dismiss(); 
       } 
      }); 
22
AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this); 
DialogInterface dia = new DialogInterface(); 

//Create a custom layout for the dialog box 
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false); 

TextView title = (TextView)layout.findViewById(R.id.rep_1_title); 
Button add_item = (Button)layout.findViewById(R.id.add_button); 

title.setText(workout_items[position]); 
dialog.setView(layout); 

AlertDialog alertDialog = dialog.create(); 

add_item.setOnClickListener(new OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     alertDialog.dismiss(); 
    } 
}); 


alertDialog.show(); 
+2

變量alertDialog是從內部類中訪問的。需要被宣佈爲最終。 – 2012-12-20 09:24:01

+0

什麼是DialogInterface dia? – 2018-01-27 15:58:14

1

我已修改了代碼PLZ檢查..

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this); 
    DialogInterface dia = new DialogInterface(); 

    //Create a custom layout for the dialog box 
    LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false); 

    TextView title = (TextView)layout.findViewById(R.id.rep_1_title); 
    Button add_item = (Button)layout.findViewById(R.id.add_button); 



    final AlertDialog Dial = alertDialog.create(); 
    title.setText(workout_items[position]); 
    Dial.setView(layout); 

    AlertDialog alertDialog = dialog.create(); 

    add_item.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Dial.dismiss(); 
     } 
    }); 


    Dial.show(); 

最新加入

最後AlertDialog Dial = alertDialog.create(); and change dialog.setView(layout);Dial.setView(layout);

現在只需要調用Dial.dismiss(); onclick listener .. 適合我。

+1

@Abdus ..爲什麼你調用create();兩次? – DJhon 2013-10-26 18:05:11

7

試試這個:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    LayoutInflater inflater = getLayoutInflater(); 
    View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null); 

    builder.setView(dialogView); 


    closeBtn = (Button)dialogView.findViewById(R.id.close_btn); 


    final AlertDialog dialog = builder.create(); 

    closeBtn .setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 

    dialog.show(); 

您應該使用AlertDialog.Builder以 「建」 的警告對話框本身。然後,在AlertDialog.Builder對象上調用create()方法。此方法返回AlertDialog對象,該對象允許您調用dismiss()。

您不應該多次創建它,也不要使用任何DialogInterface對象。這對我有用。讓我知道它是如何爲你。

+0

您的解釋稱爲create()是我正在尋找的。 – zeeshan 2015-02-17 20:44:38

0

您需要實現DialogInterface.OnClickListener而不是View.OnClickListener。那麼dialog.dismiss()將可用。

new DialogInterface.OnClickListener(){ 
public void onClick(DialogInterface dialog, int which){ 
     dialog.dismiss(); 
} 
} 
1

使用此代碼在類:

Dialog dialog = new Dialog(context); 
dialog.setContentView(R.layout.custom); 
((Button) dialog.findViewById(R.id.button)) 
    .setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     dialog.dismiss(); 
    } 
}); 
dialog.show(); 

並創建自定義。XML,然後粘貼此代碼裏面:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" Ok "/> 
</RelativeLayout> 

來源:https://www.mkyong.com/android/android-custom-dialog-example/

,如果你不喜歡上面的代碼,所以見下面的鏈接: http://thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/