使用EditText的TextView打开对话框

时间:2011-05-09 13:29:58

标签: android dialog textview android-edittext

在Android中我想创建一个TextView当我们点击那个带有edittext的打开对话框时。

1 个答案:

答案 0 :(得分:7)

TextView mClientName;
mClientName = (TextView) findViewById(R.id.EnterName);
mClientName.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View mView, MotionEvent mMotionEvent) {
    getClientNameDialog();
    return false;
}});

private void getClientNameDialog() {
    View mView = View.inflate(Aura.this, R.layout.getclientname, null);
    mSavedClientName = ((EditText) mView.findViewById(R.id.GetClientName));

    final InputMethodManager mInputMethodManager = (InputMethodManager) Aura.this
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    mInputMethodManager.restartInput(mView);

    AlertDialog.Builder mBuilder = new AlertDialog.Builder(Aura.this);
    mBuilder.setTitle(getString(R.string.enterclientname));
    mBuilder.setPositiveButton(getString(R.string.save), new Dialog.OnClickListener() {
        public void onClick(DialogInterface mDialogInterface, int mWhich) {
        mGetClientNameString = SavedClientName.getText().toString().trim();
        if (mGetClientNameString.length() > 0) {
            mClientName.setText(mGetClientNameString);
            mInputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                mDialogInterface.dismiss();
        }
      }
    });
    mBuilder.setView(mView);
    mBuilder.show();

    if (mInputMethodManager != null) {
    mInputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}