2011-04-05 64 views
3

我的AlertDialog中无法看到“我的数据”,因为它被EditText重叠。有没有在所有的,因为它正在被改写 -使用文本视图和编辑文本提示对话框间距问题

LinearLayout layout = new LinearLayout(this); 
layout.setOrientation(Vertical); 

      final TextView tx = new TextView(this); 
      tx.setText("MY DATA"); 
      layout.addView(tx); 

      // Set an EditText view to get user input 
       final EditText input = new EditText(this); 
       layout.addView(input); 

alert.setView(layout); 

回答

5

试试这个你的EditText。交换您拨打setView的顺序以了解我的意思。

要将多个视图用作自定义视图,您需要创建ViewGroup(如LinearLeayout),将EditText和TextView添加到该组,然后将该组设置为您的自定义对话框视图。

// Create a ViewGroup to hold the other views 
LinearLayout group = new LinearLayout(this); 
group.setOrientation(LinearLayout.VERTICAL); 

// Header for the TextView 
final TextView txHeader = new TextView(this); 
txHeader.setText("TEXTVIEW HEADER"); 
group.addView(txHeader); 

// Add the textview to the group 
final TextView tx = new TextView(this); 
tx.setText("MY DATA"); 
group.addView(tx); 

// Header for the EditText 
final TextView inputHeader = new TextView(this); 
inputHeader.setText("EDIT-TEXT HEADER"); 
group.addView(inputHeader); 

final EditText input = new EditText(this); 
group.addView(input); 

// Set the group as the custom dialog view 
alert.setView(group); 
+0

谢谢!它的工作。但是,如果我想在TextView之前有一个文本消息(我试图alert.setMessage(“FOLL是数据”);)然后我想在EditText之前(我添加alert.setMessage(“输入新数据”);) ,但我只看到最后一个可见,任何想法? – m4n07 2011-04-05 09:13:49

+0

首先决定你的对话需要什么样的视图,然后开始在线性布局中逐一添加这些视图。 – mudit 2011-04-05 09:15:39

1

只能设置一个视图与setView所以它不是“我的数据”不可见:

  Context context = MyActivity.this; 
      AlertDialog.Builder alert = new AlertDialog.Builder(context); 

      alert.setTitle(" NEW TITLE"); 
      alert.setMessage("MESSAGE 1"); 

      final TextView tx = new TextView(this); 
      tx.setText("MY DATA"); 
      alert.setView(tx); 

      // Set an EditText view to get user input 
       final EditText input = new EditText(this); 
       alert.setView(input); 


      alert.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked OK so do some stuff */ 
       } 
      }); 

      alert.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked Cancel so do some stuff */ 
       } 
      }); 

      alert.create(); 
      alert.show(); 
+0

请不要复制粘贴答案,只需再次回复! – mudit 2011-04-05 08:58:29

+0

谢谢!有效。但是,如果我想在TextView之前有一个文本消息(我试图alert.setMessage(“FOLL是数据”);)然后我想在EditText之前(我添加alert.setMessage(“输入新数据”);) ,但我只看到最后一个可见,任何想法? – m4n07 2011-04-05 09:09:59

+0

您也只能有一条消息。根本不要调用'setMessage',而是在我的回答中使用编辑的代码 – 2011-04-05 09:14:46

0

我建议使用自定义对话框中,定义一个XML布局如:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/plausi" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center_vertical" 
android:background="@color/tree_normal_back" 
android:padding="3dip"> 
<TextView 
    android:id="@+id/plausi_tv" 
    android:layout_width="fill_parent" 
    android:textSize="22dip" 
    android:padding="2dip" 
    android:layout_height="wrap_content" 
    android:background="@color/tree_normal_back" 
    android:textColor="@color/tree_normal_font" 
    android:minLines="4"/> 
<EditText 
    android:layout_below="@+id/plausi_tv" 
    android:id="@+id/plausi_et" 
    android:layout_width="fill_parent" 
    android:textSize="22dip" 
    android:padding="2dip" 
    android:layout_height="wrap_content" 
    android:textColor="@color/tree_input_font"/> 
</RelativeLayout>  

,并建立类似的对话框:

LayoutInflater mInflater = LayoutInflater.from(tab3.this); 
        AlertDialog.Builder builder = new AlertDialog.Builder(tab3.this); 
        builder.setTitle("Plausibilitätscheck"); 

        View convertView = mInflater.inflate(R.xml.plausi, null); 
        RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.plausi); 
        final EditText et = (EditText) convertView.findViewById(R.id.plausi_et); 
        final TextView tv = (TextView) convertView.findViewById(R.id.plausi_tv); 
        tv.setText(Html.fromHtml(vorgabe)); 
        et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED); 
        et.setText(GlobalVars.form_adapter.DATA[clicked_index]); 
        et.addTextChangedListener(new TextWatcher(){ 
         @Override 
         public void afterTextChanged(Editable arg0) { 
          // TODO Auto-generated method stub 

         } 

         @Override 
         public void beforeTextChanged(CharSequence arg0, 
           int arg1, int arg2, int arg3) { 
          // TODO Auto-generated method stub 

         } 

         @Override 
         public void onTextChanged(CharSequence arg0, 
           int arg1, int arg2, int arg3) { 
          // TODO Auto-generated method stub 

         } 

        }); 
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
           // TODO Auto-generated method stub 

          } 

         } 
        }); 
        builder.setView(rl); 


        final AlertDialog alert = builder.create(); 
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
          @Override 
          public void onFocusChange(View v, boolean hasFocus) { 
           if (hasFocus) { 
            alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
           } 
          } 
         }); 
        alert.show(); 
0

试试这个代码

“LayoutInflater factory = LayoutInflater.from(nextScreen.this); final查看textEntryView = facto ry.inflate(R.layout.text_entry,null);
最终AlertDialog.Builder meeting = new AlertDialog.Builder(nextScreen.this); meeting.setView(textEntryView).setTitle(“Ok”); meeting.setPositiveButton( “创建”,新DialogInterface.OnClickListener(){

   @Override 
       public void onClick(DialogInterface dialog, int which) { 
        TextView txt=(TextView) textEntryView.findViewById(R.id.username_edit); 
        Toast.makeText(nextScreen.this, txt.getText().toString(), Toast.LENGTH_SHORT); 
             } 
      }); 
      meeting.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 


       } 
      }); 
      meeting.create(); 
      meeting.show();" 

,并在.xml文件

<EditText 
    android:id="@+id/username_edit" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_marginLeft="20dip" 
    android:layout_marginRight="20dip" 
    android:scrollHorizontally="true" 
    android:autoText="false" 
    android:capitalize="none" 
    android:gravity="fill_horizontal" 
    android:singleLine="true"/> 

相关问题