2017-09-05 79 views
0

在我的MainActivity中我有2个Buttons来调用2个不同的对话框。第一个工作得很好,但第二个自己替换MainActivity。所以我显示了它2次,而不是MainActivity,并在常规的对话窗口中显示。 我不知道为什么会是这样的代码是完全一样的:Android对话框问题

在第一个对话框的MainActivity.java

代码:

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(); 
        } 

     } 

与第二:

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){ 
};} 

回答

0

你做

a.setContentView(R.layout.dialogedit); 

从而设置布局,你的活动主视图。相反,做

setContentView(R.layout.dialogedit)

+0

是的,这可能是所有其他“一”的,你应该删除这些也相同。如果这能解决它,你可能想接受我的答案。 – Frank