如何为Android中的Alert Dialog中的TextView和EditText设置视图?

时间:2013-02-11 09:55:18

标签: android

我使用下面的代码为表格布局设置警报对话框的TextView和EditText。 但我没有进入UI。

  LinearLayout layout1 = new LinearLayout(SimpleListViewActivity.this);
layout1.setOrientation(LinearLayout.VERTICAL);

final EditText nameEdt = new EditText(this);
final EditText nameEdt1 = new EditText(this);
final EditText nameEdt2 = new EditText(this);
final EditText nameEdt3 = new EditText(this);

LinearLayout.LayoutParams TxtLayoutParams = new LinearLayout.LayoutParams(50,50);
TableRow.LayoutParams rowparams = new TableRow.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 50);
nameEdt.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
nameEdt1.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
nameEdt2.setLayoutParams(new ViewGroup.LayoutParams(20,20));
nameEdt3.setLayoutParams(new ViewGroup.LayoutParams(20, 20));

TextView groupTxt = new TextView(SimpleListViewActivity.this);
groupTxt.setLayoutParams(TxtLayoutParams);
groupTxt.setText("Group");
TextView clientTxt = new TextView(SimpleListViewActivity.this);
clientTxt.setLayoutParams(TxtLayoutParams);
clientTxt.setText("Client");
TextView docTxt = new TextView(SimpleListViewActivity.this);
docTxt.setLayoutParams(TxtLayoutParams);
docTxt.setText("Doc_type");
TextView nameTxt = new TextView(SimpleListViewActivity.this);
nameTxt.setLayoutParams(TxtLayoutParams);
nameTxt.setText("Name");

TableRow grouprow = new TableRow(SimpleListViewActivity.this);
grouprow.setLayoutParams(rowparams);
TableRow clientrow = new TableRow(SimpleListViewActivity.this);
clientrow.setLayoutParams(rowparams);
TableRow docrow = new TableRow(SimpleListViewActivity.this);
docrow.setLayoutParams(rowparams);
TableRow namerow = new TableRow(SimpleListViewActivity.this);
namerow.setLayoutParams(rowparams); // Fixed By Praveen

grouprow.addView(groupTxt);
grouprow.addView(nameEdt3);
clientrow.addView(clientTxt);
clientrow.addView(nameEdt2);
docrow.addView(docTxt);
docrow.addView(nameEdt1);
namerow.addView(nameTxt);
namerow.addView(nameEdt);

layout1.addView(grouprow);
layout1.addView(clientrow);
layout1.addView(docrow);
layout1.addView(namerow);



myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        // do nothing 
            System.out.println("Inside Ok");
        }
    });

myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
    // do nothing
    }
    });
myDialog.setView(layout1);
AlertDialog alertDialog = myDialog.create();
alertDialog.show();
//alertDialog.getWindow().setLayout(200, 800);
    }

输出用户界面是:

enter image description here

请帮助我如何设置视图?

4 个答案:

答案 0 :(得分:1)

最好为Dialog创建一个布局文件,然后在创建Dialog时对其进行充气。

dialog.xml

<LinearLayout...
        <TextView
              android:id="@+id/textView1
        .../>
        <TextView
             android:id="@+id.textView2
        ../>
</LinearLayout>

创建对话框时。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog, null));

TextView txtView1 = (TextView) ThisActivity.this.getDialog().findViewById(R.id.textView1);
//do your TextView stuff, similar for the other views inside the layout defined earlier

答案 1 :(得分:0)

            <LinearLayout
                android:id="@id/userBet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textfield_bg"
                android:clickable="true"
                android:gravity="right|center"
                android:paddingRight="7.0dip" >

                <TextView
                    android:id="@id/userBetTxt"
                    style="@style/_005_Carrello_0_RightTicketItem"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center" />
            </LinearLayout>

我没有使用文本字段,也许这对你来说很有趣 android:background =“@ drawable / textfield_bg”将你的图像放在那里

答案 2 :(得分:0)

尝试使用自定义对话框。它很容易使用。

 final Dialog custon_dialog = new Dialog(YourActivity.this);
 custon_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 custon_dialog.setContentView(R.layout.forget_custom_dialog);
 custon_dialog.setCancelable(true);
  //Declare your textview and editbox
 Button submit_Btn = (Button) custon_dialog
                    .findViewById(R.id.submit);
 submit_Btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub


                }

            });

            custon_dialog.show();

答案 3 :(得分:0)

Activity mActivity;
String title;
TextView addresstv;
TextView dateandtimetv;
Button submit;
Button edit;
AlertDialog.Builder alertbuilder;
AlertDialog alert;

alertbuilder=new AlertDialog.Builder(mActivity);

alert=alertbuilder.create();
    LayoutInflater inflater = mActivity.getLayoutInflater();
    //View view= LayoutInflater.from(mContext).inflate(R.layout.comformdateandtime,mActivity,false);
    View view=inflater.inflate(R.layout.comformdateandtime,null);
    alert.setView(view);
    addresstv=(TextView)view.findViewById(R.id.address);
    dateandtimetv=(TextView)view.findViewById(R.id.dtime);
    submit=(Button) view.findViewById(R.id.submit);
    edit=(Button)view.findViewById(R.id.edit);