2011-12-15 71 views
0

我想添加到文本字段到对话框,但它似乎并没有工作......这是我的代码。任何人都可以修复我的代码或给我一些关于如何将两个文本字段添加到对话框的指导?Android - 如何将两个文本字段添加到对话框

谢谢。

  final EditText input1 = new EditText(this); 
     input1.setText("note"); 
     input1.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       input1.setText(""); 
      } 
     }); 
     final EditText input2 = new EditText(this); 
     input2.setMinLines(1); 
     input2.setText("0.0"); 
     input2.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       input2.setText(""); 
      } 
     }); 
     LinearLayout layout = new LinearLayout(this); 
     layout.setOrientation(1); 
     layout.addView(input1); 
     layout.addView(input2); 


     return new AlertDialog.Builder(this). 
     // code for showing Ok and Cancel button 
     .setView(layout).create(); 

回答

3

我个人使用一个新的活动,当我需要的不仅仅是对话的几个字。如果添加

<activity android:theme="@android:style/Theme.Dialog"> 
</activity> 

它会显示为一个对话框,允许您使用多个TextViews,按钮,ImageViews,纱厂等

编辑

它添加到您androidmanifest文件你想成为一个对话框ativity,这样

<activity android:name=".About" android:label="@string/app_name" 
     android:theme="@android:style/Theme.Dialog"> 
     <intent-filter> 
      <action android:name="com.example.ABOUT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
+0

+1我认为这不是定制警报对话框的最佳方式。 – 2011-12-15 06:14:51

+0

我很抱歉,我是一名初学者到android编程。我应该在哪里创建该XML?在res /布局下? – user826323 2011-12-15 06:22:58

0

尝试添加这样

final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    Context c = getBaseContext(); 
    // Create the text field in the alert dialog... 
    text = new EditText(this); 
    text.setSingleLine();  

    // Add text to dialog 
    alertDialog.setView(text); 
    alertDialog.setTitle("Alert"); 
     alertDialog.setMessage("error"); 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) {          

      } 
     });   
     alertDialog.show();   
    } 
5

创建XML布局文件

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:gravity="center_horizontal" android:background="#ffffff" 
    android:orientation="vertical" android:padding="10dp"> 
    <EditText android:id="@+id/text1" android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <EditText android:id="@+id/text2" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

你的对话框代码

final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
View dialog_layout = getLayoutInflater().inflate(R.layout.dialog_layout, null); 
// Create the text field in the alert dialog... 
EditText text1 = (EditText) dialog_layout.findViewById(R.id.text1); 
EditText text2 = (EditText) dialog_layout.findViewById(R.id.text2);   

alertDialog.setView(dialog_layout); 
alertDialog.show(); 

您可以在布局按照您的要求添加更多的控制

相关问题