2015-05-14 103 views
1

我的對話框是一個帶有一個標題和兩個按鈕的自定義佈局dialogbox_solution。 這裏是唯一的主線XML文件:編程如何在自定義對話框中設置自定義按鈕?

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout> 

    <TextView 
     android:id="@+id/txtDiaMsg" 
     /> 

    <TableLayout 
     android:layout_below="@+id/txtDiaMsg"> 
     <TableRow> 
      <Button 
       android:id="@+id/yesButton" 
       android:text=" YES "/> 
      <Button 
       android:id="@+id/noButton" 
       android:text=" NO "/> 
     </TableRow> 
    </TableLayout> 
</RelativeLayout> 

如何鏈接我的自定義按鈕,正面和負面的按鈕?

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           switch (which){ 
            case DialogInterface.BUTTON_POSITIVE: 
             //Yes button clicked 
             // do something here 
             break; 

            case DialogInterface.BUTTON_NEGATIVE: 
             //No button clicked 
             break; 
           } 
          } 
         }; 
         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
         LayoutInflater li = getLayoutInflater(); 
         View view = li.inflate(R.layout.dialogbox_solution, null); 
         builder.setView(view); 
         builder.show(); 

回答

3

這樣的財產以後創建...首先創建佈局的xml文件...用於如:dialog.xml ...,然後調用它像下面的代碼,無論你想...

final Dialog myDialog = new Dialog(this); 
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     myDialog.setContentView(R.layout.dialog); 
     myDialog.setCancelable(false); 
     Button yes = (Button) myDialog.findViewById(R.id.share); 
     Button no = (Button) myDialog.findViewById(R.id.no); 
     no.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Do your code here 
      } 
     }); 
     yes.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Do your code here 
      } 
     }); 

     myDialog.show(); 
     myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { 
      @Override 
      public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { 
       if (keyCode == KeyEvent.KEYCODE_BACK) { 
        dialog.cancel(); 
        return true; 
       } 
       return false; 
      } 
     }); 

dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:background="#e7e7e9"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/title" 
    android:id="@+id/textView10" 
    android:textSize="20sp" 
    android:textColor="#000000" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 


<Button 
    android:layout_width="120dp" 
    android:layout_height="40dp" 
    android:text="@string/yes" 
    android:id="@+id/share" 
    android:background="@drawable/loginbutton" 
    android:textSize="12sp" 
    android:gravity="center_vertical|center_horizontal" 
    android:layout_gravity="left|bottom" 
    android:layout_marginTop="30dp" 
    android:layout_below="@+id/textView15" 
    android:layout_alignLeft="@+id/textView15" 
    android:layout_alignStart="@+id/textView15" /> 

<Button 
    android:layout_width="120dp" 
    android:layout_height="40dp" 
    android:text="@string/no" 
    android:id="@+id/no" 
    android:layout_gravity="bottom|right" 
    android:background="@drawable/loginbutton" 
    android:textSize="12sp" 
    android:gravity="center_vertical|center_horizontal" 
    android:layout_alignTop="@+id/share" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

    </RelativeLayout> 
+0

這對我的工作,THXü:) –

+0

快樂幫... :) –

相關問題