2017-09-05 4 views
0

Dans mon MainActivity j'ai 2 Buttons pour appeler 2 dialogue différent. Le premier fonctionne très bien mais le second remplace le MainActivity par lui-même. donc je le montre 2 fois, au lieu du MainActivity et dans la fenêtre de dialogue normale. Je ne sais pas pourquoi il est comme que les codes sont exactement les mêmes:Android Dialog question

Dans le code MainActivity.java

de la première boîte de dialogue:

public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()){ 
      case R.id.action_set: { 
        SetDialog setDialog = new SetDialog(MainActivity.this); 
        setDialog.setContentView(R.layout.settings); 
        setDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
        setDialog.show(); 
        } 

     } 

et le second:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      AddDialog addDialog = new AddDialog(MainActivity.this); 
      addDialog.setContentView(R.layout.dialogedit); 
      addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
      addDialog.show(); 
     } 
    }); 

SetDialog.java:

public class SetDialog extends Dialog implements android.view.View.OnClickListener { 

     Activity a; 
     Spinner spinTheme, spinView; 
     Button b 

utCancel, butApply; 

    public SetDialog(Activity c) { 
     super(c); 
     this.a = c; 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 
     spinTheme =(Spinner) findViewById(R.id.spinnerTheme); 
     spinView =(Spinner)findViewById(R.id.spinnerView); 
     butApply =(Button)findViewById(R.id.buttonApplySet); 
     butCancel = (Button)findViewById(R.id.buttonCancelSet); 
    } 

    @Override 
    public void onClick(View v){} 

AddDialog.java:

public class AddDialog extends Dialog implements android.view.View.OnClickListener{ 
    Activity a; 
    Dialog d; 
    Button add , cancel; 
    RadioButton owes,lent ,money,things ; 
    EditText name ,amount,object,note; 
    Spinner spin; 

public AddDialog(Activity c) { 
    super(c); 
    this.a = c; 
} 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    a.setContentView(R.layout.dialogedit); 
    add=(Button)a.findViewById(R.id.buttonAdd); 
    cancel=(Button)a.findViewById(R.id.buttonCancel); 
    owes = (RadioButton) a.findViewById(R.id.radioButtonOwes); 
    lent = (RadioButton) a.findViewById(R.id.radioButtonLent); 
    money = (RadioButton)a.findViewById(R.id.radioButtonAmount); 
    things =(RadioButton) a.findViewById(R.id.radioButtonThings); 
    name = (EditText) a.findViewById(R.id.editName); 
} 

@Override 
public void onClick(View v){ 
};} 

Répondre

0

Vous ne

a.setContentView(R.layout.dialogedit); 

établissant ainsi la mise en page que vos activités de vue principale. Au lieu de cela faire

setContentView(R.layout.dialogedit)

+0

oui, il est probablement la même chose pour tous les autres « a. » L ', vous devez supprimer ces composants. Si cela résout, vous pouvez accepter ma réponse. – Frank