0

J'ai 2 fichiers de mise en page, un pour la mise en page principale montrant (de activity_main.xml) et une autre permet de montrer de dialogue personnalisée (de dialog_custom.xml). Les 2 fichiers sont comme suit-Android Alerte personnalisée avec XML - Cliquez sur le bouton d'action

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <Button android:id="@+id/btn_1_option" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Show Alert with 1 Button" 
      android:padding="10dip" 
      android:layout_marginTop="40dip"> 
     </Button> 

     <Button android:id="@+id/btn_2_option" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Show Alert With 2 Buttons" 
      android:padding="10dip"> 
     </Button> 

     <Button android:id="@+id/btn_3_option" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Show Alert with 3 Buttons" 
      android:padding="10dip"> 
     </Button> 

     <Button android:id="@+id/btn_custom_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Show Alert with Custom Layout - From XML" 
      android:padding="10dip"> 
     </Button> 

    </LinearLayout> 

dialog_custom.xml

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

     <TextView 
      android:id="@+id/dialog_info" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:background="@android:color/holo_orange_dark" 
      android:text="@string/dialog_text"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_below="@id/dialog_info"> 

      <Button 
       android:id="@+id/dialog_cancel" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="0.50" 
       android:background="#3333" 
       android:text="Cancel"/> 

      <Button 
       android:id="@+id/dialog_ok" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="0.50" 
       android:background="#888888" 
       android:text="Agree"/> 
     </LinearLayout> 
    </RelativeLayout> 

Et ce que je l'ai fait dans la classe principale est -

public class Activity_Main extends Activity 
    { 
     Context context = Activity_Main.this; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.dialog_main); 
      dialog.setTitle(R.string.dialog_title); 
      dialog.show(); 
     } 
    } 

Donc, je reçois une boîte de dialogue comme this-

Output

Maintenant, je veux ajouter l'option de clic pour cette ANNULE et ACCEPTEZ bouton. Mais si je veux, je suis forcé de fermer. Quelqu'un peut-il m'aider s'il vous plaît, comment puis-je le faire?

Merci de votre aide.

Répondre

1

Est-ce comme suit:

final Dialog dialog = new Dialog(this); 
dialog.setContentView(R.layout.layout.dialog_main); 
Button btCancel = (Button) dialog.findViewById(R.id.dialog_cancel); 
Button btOk = (Button) dialog.findViewById(R.id.dialog_ok); 
btCancel.setOnClickListener(new View.OnClickListener() { 
@Override 
    public void onClick(View v) { 
    //Cancel Click 

    } 
}); 
btOk.setOnClickListener(new View.OnClickListener() { 
@Override 
    public void onClick(View v) { 
    //OK CLICK  

    } 
}); 
dialog.show(); 

Si vous voulez ajouter Buttons ou tout Widget (à l'intérieur du Dialog) assurez-vous que vous trouvez la vue avec dialog.findViewById(R.id.ID_WIDGET) sinon il va se planter.

0

Je ne pouvais pas trouver votre code de clic et annuler d'accord, mais essayez ceci:

Button cancel=(Button)dialog.findViewById(R.id.dialog_cancel); 
cancel.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
//    do your work 

      } 
     }); 

Merci si le problème persiste alors votre erreur poster, puis cliquez sur le code.

0

Vous devrez peut-être ajouter View.OnClickListener aux boutons de la boîte de dialogue.

Button btnCancel = (Button) dialog.findViewById(R.id.dialog_cancel); 
Button btnOk = (Button) dialog.findViewById(R.id.dialog_ok); 
btnCancel.setOnClickListener(new View.OnClickListener() { 
@Override 
    public void onClick(View v) { 
    //Called on click of Cancel button 
    } 
}); 
btnOk.setOnClickListener(new View.OnClickListener() { 
@Override 
    public void onClick(View v) { 
    //Called on click of Cancel button  
    } 
}); 
dialog.show();