如何使用AlertDialog在主上下文中更改EditText提示

时间:2012-08-16 06:15:50

标签: android

private EditText result = (EditText) findViewById(R.id.editText1);
final String[] dropdownList ={"a123", "b456"};

public void onCreate(...) {
    ...
    alert();
}


private void alert() {
    new AlertDialog.Builder(this)
    .setTitle("select")
    .setSingleChoiceItems(dropdownList, checkedItem, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.setHint("some tips");
                }
        )}.show()

EditText结果已在layout.xml中有提示,但我想更改

中的提示

EditText结果,但上面的代码抛出NullPointerException,有什么问题?

编辑:

public class AAA extends Activity {

private EditText result;
private String[] dropdownList = {"A123", "A456"};
private int checkedItem = 0; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    result = (EditText) findViewById(R.id.editText1);
    alert();
}

private void alert() {
    new AlertDialog.Builder(this)
    .setTitle("select")
    .setSingleChoiceItems(dropdownList, checkedItem, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    result.setHint("some tips");
                }
    }).show();
}

}

1 个答案:

答案 0 :(得分:1)

使用处理程序从alertDialog更新任何UI组件。

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
       result.setHint("some tips");
    }
}

private void alert() {
    new AlertDialog.Builder(this).setTitle("select")
    .setSingleChoiceItems(dropdownList, checkedItem, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            handler.sendEmptyMessage(0);
        }
    }).show();
}