gpt4 book ai didi

android - 自定义对话框安卓

转载 作者:行者123 更新时间:2023-11-30 04:19:50 27 4
gpt4 key购买 nike

我正在尝试显示一个可自定义的对话框。此对话框包含 3 个编辑文本和一个时间选择器。当我在屏幕上按下一个按钮时,我想显示这个对话框。我查看了谷歌教程并尝试编写代码。但是,当我使用 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();

}
});

在此之后,我编辑代码并添加两个按钮。我想将警报对话框的结果获取到所看到的 Activity 。我写的代码在下面。

公共(public)类 HapHatirlatici 扩展 Activity{

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

}

最佳答案

您忘记了 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 替换为您的实际 Activity 名称。

关于android - 自定义对话框安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9446206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com