Java 自定义AlertDialog无法在onCreate()中初始化
我试图通过扩展Java 自定义AlertDialog无法在onCreate()中初始化,java,android,android-alertdialog,oncreate,Java,Android,Android Alertdialog,Oncreate,我试图通过扩展AlertDialog类来构建一个自定义的AlertDialog。 像往常一样,我正在其onCreate()方法中设置对话框。或者,我正试图这样做: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("Some title"); this.setButton(BUTTON_POSIT
AlertDialog
类来构建一个自定义的AlertDialog
。像往常一样,我正在其
onCreate()方法中设置对话框。或者,我正试图这样做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Some title");
this.setButton(BUTTON_POSITIVE, "Click me", (DialogInterface.OnClickListener)null);
final FrameLayout custom = (FrameLayout) this
.findViewById(android.R.id.custom);
custom.addView(this.getLayoutInflater().inflate(R.layout.mydlg, null),
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
现在,当显示此对话框的实例时,什么都不显示当前的活动
淡出并失去焦点,但我的对话框中没有一个像素显示出来。按Back键将活动
带回到前台,向我表明实际上显示了一个对话框,但只是一个完全空的对话框
但是,当我创建一个AlertDialog
并使用,例如,dlg.setButton(按钮为正,“单击我”,“DialogInterface.OnClickListener”为空)代码>
对话框将显示相应的按钮。
即使我使用与上面相同的代码在其构造函数中设置自定义对话框,一切似乎都正常
这怎么可能?为什么我不能在对话框的onCreate()方法中初始化对话框?这不是初始化任何GUI元素的方式吗?我错过了什么
编辑
请注意,某些内容已“显示”,淡出活动并将注意力从中移开。只是它看起来完全是空的/看不见的
这里是另一种尝试:
this.setTitle("Some title");
this.setButton(BUTTON_POSITIVE, "Click me", (DialogInterface.OnClickListener)null);
final View v = this.getLayoutInflater().inflate(R.layout.mydlg, null);
this.setView(v);
这些精确的行在放入对话框的构造函数中时确实起作用。
这些精确的行在放入我的对话框的onCreate()
时不起作用
这是怎么回事
通常情况下,我不应该在onCreate()
中这样做吗如果我转而在构造函数中执行上述初始化,是否会遇到麻烦?(这对我来说似乎并不太干净。) 你需要调用<代码>显示()(代码)>方法来查看一些东西。 你需要调用<代码>显示()(代码)>方法来查看一些东西。 你应该考虑使用子类AdvtTalk本身代替子类。它允许您完成示例中需要的所有操作(顺序为:setTitle()、setPositiveButton()和setView()。不要忘记在最后调用create()来实际获取对话框
另外,检查onCreateDialog()和onPrepareDialog()活动方法是否正确实现。如果根本没有实现它们(非托管对话),那么请考虑这样做,特别是如果应用程序允许定位更改的话。您可能知道这一点,但这里有一个教程:
此外,DialogFragments是一种更简单的实现方法,但您需要更新的API版本或兼容包:
最后一个问题-在活动中调用show()的位置是什么?OnReSuMe()应该是OK,OnCurATE()不是那么多。< P>您应该考虑使用子类AdvutalCudio本身来代替。它允许您完成示例中需要的所有操作(顺序为:setTitle()、setPositiveButton()和setView()。不要忘记在最后调用create()来实际获取对话框
另外,检查onCreateDialog()和onPrepareDialog()活动方法是否正确实现。如果根本没有实现它们(非托管对话),那么请考虑这样做,特别是如果应用程序允许定位更改的话。您可能知道这一点,但这里有一个教程:
此外,DialogFragments是一种更简单的实现方法,但您需要更新的API版本或兼容包:
最后一个问题-在活动中调用show()的位置是什么?onResume()应该可以,onCreate()没有那么多。对不起,我来晚了:)
对于警报对话框,您必须使用不同的方式
我的做法是在创建警报对话框之前自定义视图:
// This is the activity that is the background of the AlertDialog
public class Main extends Activity {
public static final int DIALOG_CONFIG = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.emptybackground);
}
@Override
protected void onStart() {
super.onStart();
// Open the alert dialog on openning the Activity
showDialog(Main.DIALOG_CONFIG );
}
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(this);
switch (id) {
case DIALOG_CONFIG:
// Here, we load the existing view R.layout.config
configView = factory.inflate(R.layout.config, null);
configDialog = new AlertDialog.Builder(this)
.setTitle("Configuration")
.setView(configView)
.create();
// Using configView, you can do whatever you want with the view. Here, we add value to a spinner.
Spinner spinner = (Spinner)configView.findViewById(R.id.config_select_conn);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("TCP");
adapter.add("Bluetooth");
spinner.setAdapter(adapter);
return configPrinter;
}
return null;
}
}
//这是作为AlertDialog背景的活动
公共类主要扩展活动{
公共静态最终int对话框_CONFIG=1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.emptybackground);
}
@凌驾
受保护的void onStart(){
super.onStart();
//打开活动时打开警报对话框
showDialog(Main.DIALOG\u CONFIG);
}
受保护的对话框onCreateDialog(int id){
LayoutFlater工厂=LayoutFlater.from(此);
开关(id){
案例对话框\u配置:
//在这里,我们加载现有的视图R.layout.config
configView=factory.inflate(R.layout.config,null);
configDialog=新建AlertDialog.Builder(此)
.setTitle(“配置”)
.setView(配置视图)
.create();
//使用configView,您可以对视图执行任何操作。
微调器微调器=(微调器)configView.findviewbyd(R.id.config_uselect\u conn);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
添加(“TCP”);
适配器。添加(“蓝牙”);
旋转器。设置适配器(适配器);
返回打印机;
}
返回null;
}
}
对不起,我来晚了:)
对于警报对话框,您必须使用不同的方式
我的做法是在创建警报对话框之前自定义视图:
// This is the activity that is the background of the AlertDialog
public class Main extends Activity {
public static final int DIALOG_CONFIG = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.emptybackground);
}
@Override
protected void onStart() {
super.onStart();
// Open the alert dialog on openning the Activity
showDialog(Main.DIALOG_CONFIG );
}
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(this);
switch (id) {
case DIALOG_CONFIG:
// Here, we load the existing view R.layout.config
configView = factory.inflate(R.layout.config, null);
configDialog = new AlertDialog.Builder(this)
.setTitle("Configuration")
.setView(configView)
.create();
// Using configView, you can do whatever you want with the view. Here, we add value to a spinner.
Spinner spinner = (Spinner)configView.findViewById(R.id.config_select_conn);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("TCP");
adapter.add("Bluetooth");
spinner.setAdapter(adapter);
return configPrinter;
}
return null;
}
}
//这是作为AlertDialog背景的活动
公共类主要扩展活动{
公共静态最终int对话框_CONFIG=1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.emptybackground);