2012-07-11 84 views
0

我們已經創建了兩個自定義對話框,一個是關於另一個是警告。當我在拖拽自定義對話框中交替選擇時,該按鈕不起作用。爲什麼自定義對話框按鈕在android中不工作

示例代碼

AlertDialog.Builder builder; 
Context mContext; 
LayoutInflater inflater; 
View layout; 
Dialog dialog; 
@Override 
protected Dialog onCreateDialog(int id) 
{ 
    switch (id) 
    { 
     case 1: 
      builder = null; 
      mContext = this; 
      inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
      layout = inflater.inflate(R.layout.alert_page, (ViewGroup) findViewById(R.id.alert_Root)); 
      Button alertUser = (Button) layout.findViewById(R.id.alert_Submit); 
      alertUser.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        try 
        { 
         dialog.dismiss(); 
        } 
        catch (Exception e) 
        { 
         Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
      builder = new AlertDialog.Builder(mContext); 
      builder.setView(layout); 
      dialog = builder.create(); 
      dialog.show(); 
      break; 

     case 2: 
      builder = null; 
      mContext = this; 
      inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
      layout = inflater.inflate(R.layout.about_page, (ViewGroup) findViewById(R.id.about_Root)); 
      Button aboutUser = (Button) layout.findViewById(R.id.about_Submit); 
      aboutUser.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Log.e("About","About"); 
        try 
        { 
         Log.e("About1","About"); 
         dialog.dismiss(); 
        } 
        catch (Exception e) 
        { 
         Log.e("About","About12"); 
         Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
      builder = new AlertDialog.Builder(mContext); 
      builder.setView(layout); 
      dialog = builder.create(); 
      dialog.show(); 
      break; 
    } 
    return dialog; 
} 

例如我正在使用兩個按鈕。第一個按鈕稱爲case 1,第二個按鈕稱爲case 2

我被選中第一個按鈕訪問case 1,然後選擇自定義對話框alertUser按鈕successfully Exit the dialog box

立即選中第二個按鈕訪問case 2,然後選擇自定義對話框aboutUser按鈕successfully Exit the dialog box

之後立即選中第一個按鈕訪問case 1,然後選擇自定義對話框alertUser按鈕Now the dialog box does not exist (button is now not working)

我在哪裏錯了代碼。如何解決這個問題。

在此先感謝。

+0

而不是佈局= inflater.inflate(R.layout.alert_page,(ViewGroup中)findViewById(R.id.alert_Root)) ;寫佈局= inflater.inflate(R.layout.alert_page,null); – AkashG 2012-07-11 07:56:32

+0

你正在調用對話框爲'showDialog(int)'?那麼爲什麼你寫這行'dialog.show();'在'onCreateDialog' – 2012-07-11 08:14:28

+0

@MMohsinNaeem我可以刪除這行'dialog.show();'後我得到同樣的問題。 – Sekar 2012-07-11 08:31:59

回答

0

您正在呼叫對話框爲showDialog(int)。刪除此行dialog.show();onCreateDialog

,並呼籲dismissDialog(int)關閉對話框,而不是dialog.dismiss();

+0

謝謝。但'showDialog(int)'和'dismissDialog(int)'已被棄用。如何解決這個問題呢。我在'developer.android提到。com'但我不知道。你能給我任何一個例子嗎?謝謝。 – Sekar 2012-07-11 11:01:09

0

這樣做:

Button button1=(Button) findViewById(R.id.btn1); 
Button button2=(Button) findViewById(R.id.btn2); 

button1.setOnClickListener(this); 
button2.setOnClickListener(this); 

落實OnClickListener的活動,並添加未實現的方法,你也會得到儘可能的onClick。

public void onClick(View v) { 
switch(v.getId()){ 
     case R.id.btn1: 
      //write code 
      break; 
     case R.id.btn2: 
      //write code 
      break; 
     } 
} 

做任何你想做的事情按鈕的點擊事件。

+0

當我試圖訪問這行代碼時,我得到了錯誤消息'按鈕button2 =(按鈕)findViewById(R.id.btn2);'錯誤消息是'方法setOnClickListener(View.OnClickListener)鍵入View不適用於參數' – Sekar 2012-07-11 08:30:50

+0

初始化您的活動onCreate中的兩個按鈕,然後設置它們的click listener.clean並在您再次卡住時生成您的project.ask – AkashG 2012-07-11 08:33:50