2013-08-03 35 views
0
  public void name() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      LayoutInflater inflater = this.getLayoutInflater(); 
      builder.setView(inflater.inflate(R.layout.unit null)); 
        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show(); 
       dialog.dismiss(); 
      } 
     }); 

     final AlertDialog dialog = builder.create(); 

     Button dialogButton = (Button) dialog.findViewById(R.id.i_pharmacy); 
     // if button is clicked, close the custom dialog 
     dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      // dialog.dismiss(); 
      } 
     }); 

     dialog.show(); 


    } 

當我運行上面的代碼時,我得到了NULL點異常。使用上面的代碼在對話框上顯示一個按鈕。在點擊對話框按鈕後,應該發生一些事件。Android無法觸發對話框上的按鈕事件

+0

「它沒有工作」?那是什麼意思? – Simon

+0

事件未被解僱。 Toast.makeText(getApplicationContext(),「AAAAAAAAAAAAa」,Toast.LENGTH_SHORT).show();行不起作用 – Dilshi

+0

View.OnClickListener = new View.OnClickListener() – KOTIOS

回答

1

嘗試這樣:

  // custom dialog 
      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.custom); 
      dialog.setTitle("Title..."); 

      // set the custom dialog components - text, image and button 
      TextView text = (TextView) dialog.findViewById(R.id.text); 
      text.setText("Android custom dialog example!"); 
      ImageView image = (ImageView) dialog.findViewById(R.id.image); 
      image.setImageResource(R.drawable.ic_launcher); 

      Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialog.dismiss(); 
       } 
      }); 

      dialog.show(); 

佈局:

<?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" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     android:layout_toRightOf="@+id/image"/>/> 

    <Button 
     android:id="@+id/dialogButtonOK" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text=" Ok " 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_below="@+id/image" 
     /> 

</RelativeLayout> 
+0

請參閱我的edit.i完成了您的更改,但直到劑量工作 – Dilshi

+0

我認爲您完全沒有做到,只需簡單地複製粘貼我的代碼並查看會發生什麼。 – fida1989

+0

這是非常棒的工作。 +1 –

0
builder.setView(inflater.inflate(R.layout.unit null)); 
這部分

。你是不是在空白之前缺少一個逗號?

相關問題