我有一个Android自定义对话框的小问题。在自定义对话框中处理按钮
我建立一个自定义对话框中的onCreateDialog(int)的函数:
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
我在同一个类中的onClick(视图)功能:
public void onClick(View v) {
switch(v.getId()) {
case R.id.dialog_button:
Log.i("pma57","dialog button pressed");
break;
case R.id.main_button:
showDialog(DIALOG_CUSTOM);
break;
}
}
这是XML定义:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/enter_username" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dialog_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"
android:onClick="onClick" />
</LinearLayout>
对话框出现。但按钮不起作用(应用程序崩溃) - 这很好,因为回调的onClick函数是在我的主要活动中定义的 - 而且对话框是一个新的活动(对吗?)。
但我真的不知道我是如何在对话框中实现一个按钮 - 我认为这是一个技术的基本理解问题。很长的路要将Dialog的子类化,然后把所有的东西都写出来 - 但还有另一种我没有看到的方式吗?
我在您的xml文件中看不到main_button。这可能是你遇到的问题。 – prolink007 2010-11-06 16:25:26
这是因为它只是程序的重要组成部分 - 主要按钮在主要活动的另一个xml文件中。 – Herrbert 2010-11-06 21:22:14