2013-02-14 118 views
4

我在編輯我的問題,以便更好地理解我需要的內容,因爲下面的回答並未給出適當的解決方案。所以,我必須做的是在底部使用中性按鈕進行自定義警報對話框。這裏是例如,它應該是什麼樣子:Android:帶有自定義對話框和中性按鈕的警報對話框

I have to make a dialog which looks like this

直到現在我一直在使用XML和改變活動android:theme="@android:style/Theme.Holo.Light.Dialog"使用的自定義對話框,這樣我就可以得到的外觀和感覺一樣。這裏是我的XML代碼:

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

    <TextView 
     android:id="@+id/login_prompt_header" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="15dp" 
     android:text="@string/login_prompt_rewards_header" 
     android:textSize="17sp" 
     android:textStyle="bold" > 
    </TextView> 

    <TextView 
     android:id="@+id/login_prompt_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/login_prompt_header" 
     android:layout_marginTop="5dp" 
     android:gravity="center_vertical|center_horizontal" 
     android:paddingLeft="18dp" 
     android:paddingRight="18dp" 
     android:text="@string/login_prompt_rewards_text" 
     android:textSize="15sp" > 
    </TextView> 

    <Button 
     android:id="@+id/log_in" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/login_prompt_text" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="15dp" 
     android:background="@drawable/log_in" 
     android:contentDescription="@string/none" > 
    </Button> 

    <Button 
     android:id="@+id/create_account" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/log_in" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/create_account" 
     android:contentDescription="@string/none" > 
    </Button> 

</RelativeLayout> 

通過上述佈局,我發現了以下結果:

Till now implemented

我現在面臨的問題是:如何設置中性按鈕自定義對話?我對此沒有任何想法。如果您對我的問題有疑問,或者您無法理解該語言,請發表評論,以便我再次給您一個清晰的想法。幫助將不勝感激。

+0

你是如何能得到它綠色的按鈕,使它看起來3D? – Si8 2013-08-15 13:57:41

+0

@ SiKni8我爲此使用了自定義drawable。 – Anupam 2013-08-15 18:31:23

回答

11

創建自定義警告對話框像

public void messageDialog(String title, String message, final Context activity) { 

     final Dialog myDialog = new Dialog(activity); 
     myDialog.setContentView(R.layout.messagescreen); 
     myDialog.setTitle(title); 
     myDialog.setCancelable(false); 

     TextView text = (TextView) myDialog.findViewById(R.id.bidmessage); 
     text.setMovementMethod(ScrollingMovementMethod.getInstance()); 
     text.setText(message); 

     Button login = (Button) myDialog.findViewById(R.id.buttonlogin); 
     login.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

       myDialog.dismiss(); 


      } 
     }); 

     Button createAccount= (Button) myDialog.findViewById(R.id.buttoncreateaccount); 
     createAccount.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

       myDialog.dismiss(); 


      } 
     }); 


     myDialog.show(); 

    } 

其中R.layout.messagescreen是您的自定義創建的佈局,爲您在圖像顯示。試試這個,讓我知道你是否面臨任何問題。

+0

此解決方案不幫助我。我想要一箇中性按鈕也使用自定義佈局來提醒對話框。你有什麼想法嗎? – Anupam 2013-02-15 05:58:48

+0

好朋友,您可以在此警報對話框中放置儘可能多的按鈕。這些按鈕可以在您的佈局中實現 – 2013-02-15 06:05:35

+0

請參閱編輯的問題,我在問什麼。順便說一句,謝謝你的努力的朋友。 – Anupam 2013-02-15 06:07:53

2

Anupam,我知道你已經選擇了一個答案,但我雖然可能會在我的2分錢中添加我如何做類似的事情。

我已經使用了AlertDialog而不是Dialog,它在被調用時浮在應用程序之上。

private void showLoginDialog() {   
    // layout and inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    View content = inflater.inflate(R.layout.dialog_settings, null); 

    AlertDialog.Builder dialog = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light)); 
    dialog.setTitle(getString(R.string.title_settings_dialog)); 
    dialog.setView(content); 

    Button butLogin = (Button) content.findViewById(R.id.log_in); 
    butLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Do your Login stuff here or all another method 
      // does not close dialog 
     } 
    }); 

    Button butAccount = (Button) content.findViewById(R.id.create_account); 
    butAccount.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Do your Create Account stuff here or all another method 
      // does not close dialog 
     } 
    }); 

    dialog.setNegativeButton(R.string.skip, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // do nothing, just allow dialog to close 
     } 
    }); 

    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
     @Override 
     public void onCancel(DialogInterface dialog) { 
      // do nothing, just allow dialog to close 
      // this happens on back button getting hit 
     } 
    }); 

    dialog.show(); 
} 
+0

這是一個更正確的答案! - 但是,最好將Builder內的DialogFragment正確覆蓋'onCreateDialog'方法。 使對話框成爲一個單獨的對象,並從活動中調用YourDialog.newInstance()。show(getFragmentManager(),YourDialog。標籤); – Davidea 2015-06-08 08:36:13

+0

乾杯,我會注意到,當我再次使用Davidea。 – Talung 2015-06-08 14:20:19

0

,你可以簡單地設置這樣的:

dialog.setNeutralButton("skip", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       //Your code 
      } 
     });