每次加载对话框时,我都无法将一些文本设置为EditText。
问题是每当对话窗口出现时,editText中应该有一些可以编辑的特定文本。它不能是静态的(在XML中定义),那么我应该在哪里放置像wrapper.setText("some phone number etc");
这样的代码?
以下是代码:
private void edit(final long id) {
if (id>0)
{
LayoutInflater inflator = LayoutInflater.from(this);
View dialogView = inflator.inflate(R.layout.editonly, null);
final editDialogContentGetter wrapper = new editDialogContentGetter(dialogView);
new AlertDialog.Builder(this).setTitle(R.string.edit).
setView(dialogView).setPositiveButton(R.string.edit,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
basicallyEdit(wrapper);
}}).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AllPhones2.this,R.string.nochange,Toast.LENGTH_SHORT).show();
}}).show();
}
}
protected void basicallyEdit(editDialogContentGetter wrapper) {
// some code here to process information recieved by the dialog
}
class editDialogContentGetter
{
//class for dialog
EditText editphoneBox = null;
View base2 = null;
public editDialogContentGetter(View base2) {
// TODO Auto-generated constructor stub
this.base2 = base2;
}
public EditText getEditBoxField()
{
if(editphoneBox==null)
{
editphoneBox =(EditText) base2.findViewById(R.id.editphone);
}
return editphoneBox;
}
public String getEditBoxText()
{
return getEditBoxField().getText().toString();
}
}
答案 0 :(得分:0)
让它工作.. 我所要做的就是在alertdialog对象上调用findViewById()并调用setText,代码就像这样
EditText ed=(EditText) editDialog.findViewById(R.id.editphone); ed.setText("1234");
非常感谢