我正在尝试显示可自定义的对话框。该对话框包含3个edittext和一个timepicker。当我按下屏幕上的按钮时,我想显示此对话框。我查看了谷歌教程并尝试编写代码。但是,当我在inflater中使用根布局作为其中正在按下按钮的布局时,它会在按钮下添加对话框。当我在dialog_xml中使用根布局时按钮不起作用。 该代码的一部分在下面。你有什么想法我怎么能以正确的方式做到这一点?
Button ekleButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.haphatirlatici);
// After creating the activity setting other things for running
ekleButton = (Button) findViewById(R.id.EkleButton);
ekleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// Dialog icin yerlesimler
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.ekle_dialog,
(ViewGroup) findViewById(R.id.Ekle_Layout));
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
}
});
在此之后,我编辑代码并添加两个按钮。我想将警报对话框的结果显示给看到的活动。 我写的代码如下。
公共类HapHatirlatici扩展了活动{
Button ekleButton;
boolean eklendiMi;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.haphatirlatici);
// After creating the activity setting other things for running
ekleButton = (Button) findViewById(R.id.EkleButton);
eklendiMi = false;
ekleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(HapHatirlatici.this);
builder.setPositiveButton(R.string.ekle,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
eklendiMi = true;
}
});
builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
eklendiMi = false;
}
});
builder.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.setTitle("Ilac Ekleme");
alertDialog.show();
}
});
}
public boolean databaseEkle()
{
boolean sonuc = false;
return sonuc;
}
}
答案 0 :(得分:1)
您忘记alertDialog.show();
实际显示您刚构建的对话框。
此外,您的代码可以简化:
Button ekleButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.haphatirlatici);
// After creating the activity setting other things for running
ekleButton = (Button) findViewById(R.id.EkleButton);
ekleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityClass.this);
builder.setView(layout);
AlertDialog alertDialog = builder.create();
// this is what you forgot:
alertDialog.show();
}
});
}
当然,请将YourActivityClass
替换为您的实际活动名称。