2013-11-09 167 views
1

我正在爲我的自定義對話框使用以下代碼。正在關閉自定義對話框

Code is here

我使用setCustomView Method.That佈局的新佈局包含「確定」按鈕和「Cancel」按鈕。

我需要關閉對話框,當點擊取消。

buttonCancel.setOnClickListener(new OnClickListener(){ 


        @Override 
        public void onClick(View arg0) { 
         // TODO Auto-generated method stub 
         Log.d("onClick" , "YYYYY"); 
         //up to this comes , here what I can wright 
        } 
       }); 
+0

請顯示完整的代碼。 –

+0

@ shree202:https://github.com/danoz73/QustomDialog ful代碼在這裏 –

+0

不是github上的代碼,而是你嘗試過。 –

回答

0

dialogObject.dismiss();

您可以使用此方法

+1

對話框對象沒有解僱方法..... –

+0

你可以把你的對話框代碼放在這裏......? – GOLDEE

+0

https://github.com/danoz73/QustomDialog –

0

試試這個:

buttonCancel.setOnClickListener(new OnClickListener(){ 


        @Override 
        public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Log.d("onClick" , "YYYYY"); 

        qustomDialogBuilder.dismiss();//this line will close the dialog 

        } 
    }); 
0

更換你TestDialogActivity像下面,

public class TestDialogActivity extends Activity { 

    private static final String HALLOWEEN_ORANGE = "#FF7F27"; 
    private AlertDialog alertDialog; 

    private OnClickListener mShowDialogClickListener = new OnClickListener() { 
     public void onClick(View v) { 

      QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(
        v.getContext()) 
        .setTitle("Set IP Address") 
        .setTitleColor(HALLOWEEN_ORANGE) 
        .setDividerColor(HALLOWEEN_ORANGE) 
        .setMessage("You are now entering the 10th dimension.") 
        .setCustomView(R.layout.example_ip_address_layout, 
          v.getContext()) 
        .setIcon(getResources().getDrawable(R.drawable.ic_launcher)); 

      alertDialog=qustomDialogBuilder.create(); 
      qustomDialogBuilder.setAlertDialog(alertDialog); 
      alertDialog.show(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button bt = (Button) findViewById(R.id.button1); 
     bt.setOnClickListener(mShowDialogClickListener); 
    } 

和替換的QustomDialogBuildersetCustomView像下面

public QustomDialogBuilder setCustomView(int resId, final Context context) { 
     View customView = View.inflate(context, resId, null); 
     ((TextView)customView.findViewById(R.id.ip_text)).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       alertDialog.dismiss(); 
      } 
     }); 
     ((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView); 
     return this; 
    } 

最後加上下面一行到您的QustomDialogBuilder

private AlertDialog alertDialog; public void setAlertDialog(AlertDialog alertDialog) { this.alertDialog=alertDialog; }

結束對IP Address文字的對話框中點擊。

0

在活動類(TestDialogActivity)使用QustomDialog Source here,您可以設置「確定」,通過設置對話框這樣正負按鈕「取消」按鈕:

private OnClickListener mShowDialogClickListener =new OnClickListener(){ 
    public void onClick(View v){ 
     QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()). 
       setTitle("Set IP Address"). 
       setTitleColor(HALLOWEEN_ORANGE). 
       setDividerColor(HALLOWEEN_ORANGE). 
       setMessage("You are now entering the 10th dimension."). 
       setCustomView(R.layout.example_ip_address_layout, v.getContext()). 
       setIcon(getResources().getDrawable(R.drawable.ic_launcher)); 

     qustomDialogBuilder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 

     qustomDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       /** 
       * Do something here... 
       */ 
      } 
     }); 
     qustomDialogBuilder.show(); 
    } 
}; 

,它會是這樣的:

enter image description here

希望你會有所幫助。謝謝!