2010-11-06 39 views
2

我有一个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的子类化,然后把所有的东西都写出来 - 但还有另一种我没有看到的方式吗?

+0

我在您的xml文件中看不到main_button。这可能是你遇到的问题。 – prolink007 2010-11-06 16:25:26

+0

这是因为它只是程序的重要组成部分 - 主要按钮在主要活动的另一个xml文件中。 – Herrbert 2010-11-06 21:22:14

回答

21

绕着它的方式,我用的,而不是一个开关块是使用onClickListeners的按钮:

dialog = new Dialog(this); 
dialog.setContentView(R.layout.custom_dialog); 
dialog.setTitle("Custom Dialog"); 


Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button); 
dialog_btn.setOnClickListener(new View.OnClickListener() 
{ 
    // Perform button logic 
} 

请注意,您从对话框中找到视图,而不是直接调用findViewById,因为它将返回空指针,因为应用程序视图上不会有dialog_button。

+0

ahhh。我尝试了这种方式,但我错过了dialog.findViewById()之前的对话框,这可能是我的正确解决方案!谢谢! – Herrbert 2010-11-06 21:21:18

+0

是的,我试了它 - 与dialog.findViewById()我能够设置侦听器到我的主要活动 - 现在就像一个魅力。再次感谢你! – Herrbert 2010-11-06 21:29:55

+0

没问题 - 最简单的方法来记住它,当你在你的活动中工作时,它会有一个视图 - 如果你调用findViewById,你会得到对视图中对象(按钮)ID的引用 - 这里的上下文是你的活动 - 如果您创建第二个视图 - 在这种情况下,您想要dialog.findViewById的对话框(可以明显命名为任何对象),因为这将从查看​​用于引用的对话视图的参考的上下文视图改变。 – Scoobler 2010-11-06 22:32:12

-2

我必须说我真的不知道你在做什么。要显示一个对话框,您可以使用AlertDialog类,并添加你通过代码所需要的按钮:

private void insertSelfPhonenumberRequestDialog() { 
    final View layout = View.inflate(this, R.layout.dialog_phonenumber_request, null); 
    ((TextView) layout.findViewById(R.id.label)).setText(getString(R.string.dialog_insert_self_phonenumber_request_text1)); 
    ((TextView) layout.findViewById(R.id.hint)).setText(getString(R.string.dialog_general_info_text1)); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setIcon(0); 
    builder.setTitle(getString(R.string.dialog_insert_self_phonenumber_request_title)); 

    builder.setPositiveButton(getString(R.string.save), new Dialog.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //whatever 
     } 
    }); 
    builder.setView(layout); 
    builder.show(); 
} 
+0

是的,我可以使用一个警告对话框 - 但这是我听到的演讲的任务 - 我必须使用自定义对话框。 – Herrbert 2010-11-06 21:20:12

相关问题