android alertdialog edittext,android – 如何使用默认的AlertDialog以编程方式添加TextView和EditText...

我一直在尝试在默认的AlertDialog中添加两个元素,但我似乎无法使它工作.这是我的代码:

// START Dialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

TextView tv = new TextView(this);

tv.setText(title);

tv.setPadding(40, 40, 40, 40);

tv.setGravity(Gravity.CENTER);

tv.setTextSize(20);

EditText et = new EditText(this);

etStr = et.getText().toString();

alertDialogBuilder.setView(et);

alertDialogBuilder.setTitle(title);

alertDialogBuilder.setMessage("Input Student ID");

alertDialogBuilder.setCustomTitle(tv);

if (isError)

alertDialogBuilder.setIcon(R.drawable.icon_warning);

// alertDialogBuilder.setMessage(message);

alertDialogBuilder.setCancelable(false);

// Setting Negative "Cancel" Button

alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

dialog.cancel();

}

});

// Setting Positive "Yes" Button

alertDialogBuilder.setPositiveButton("OK",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

if (isError)

finish();

else {

Intent intent = new Intent(

ChangeDeviceActivity.this,

MyPageActivity.class);

startActivity(intent);

}

}

});

AlertDialog alertDialog = alertDialogBuilder.create();

try {

alertDialog.show();

} catch (Exception e) {

// WindowManager$BadTokenException will be caught and the app would

// not display the 'Force Close' message

e.printStackTrace();

}

现在,这只是一个带有alertDialogBu​​ilder.setMessage设置消息的EditText(“输入学生ID”);但是我想把它变成TextView,这样我就能证明它是正确的.我该怎么做呢?

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

LinearLayout layout = new LinearLayout(this);

LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layout.setOrientation(LinearLayout.VERTICAL);

layout.setLayoutParams(parms);

layout.setGravity(Gravity.CLIP_VERTICAL);

layout.setPadding(2, 2, 2, 2);

TextView tv = new TextView(this);

tv.setText("Text View title");

tv.setPadding(40, 40, 40, 40);

tv.setGravity(Gravity.CENTER);

tv.setTextSize(20);

EditText et = new EditText(this);

etStr = et.getText().toString();

TextView tv1 = new TextView(this);

tv1.setText("Input Student ID");

LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

tv1Params.bottomMargin = 5;

layout.addView(tv1,tv1Params);

layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

alertDialogBuilder.setView(layout);

alertDialogBuilder.setTitle(title);

// alertDialogBuilder.setMessage("Input Student ID");

alertDialogBuilder.setCustomTitle(tv);

if (isError)

alertDialogBuilder.setIcon(R.drawable.icon_warning);

// alertDialogBuilder.setMessage(message);

alertDialogBuilder.setCancelable(false);

// Setting Negative "Cancel" Button

alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

dialog.cancel();

}

});

// Setting Positive "OK" Button

alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

if (isError)

finish();

else {

Intent intent = new Intent(ChangeDeviceActivity.this,

MyPageActivity.class); startActivity(intent);

}

}

});

AlertDialog alertDialog = alertDialogBuilder.create();

try {

alertDialog.show();

} catch (Exception e) {

// WindowManager$BadTokenException will be caught and the app would

// not display the 'Force Close' message

e.printStackTrace();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值