editText in android
I am making an application and I have added an EditText in my layout.
What I开发者_如何转开发'm trying to achieve is that when EditText is selected should open a dialog box, with some general text and an OK
button. Which method can I use to accomplish this?
You must try some thing like this
searchtext.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
showDialog();
}
}
};
and here goes showDialog() code
private void showDialog()
{
final AlertDialog.Builder Main_Dialog = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setPadding(10, 10, 10, 10);
input.setText(ipAddress);
Main_Dialog.setView(input);
Main_Dialog.setTitle("Enter IP Address:");
Main_Dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//Do whatever
}
});
Main_Dialog.show();
}
If I'm reading your dilemma correctly, you'd set an OnFocusChangeListener for the EditText. Something like the following snip.
EditText e = new EditText(this);
e.setOnFocusChangeListener
(
new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.hasFocus()) {
showDialog(COOL_DIALOG);
}
}
}
);
Note, this seems like a somewhat odd user interaction. Why not just have them use the EditText for what it was made for? If you really want to do this, I'd suggest using a regular TextView or Button and use a click handler to pop the dialog. This would create a more "expected" interaction for the user.
You mean you want to display the dialog when your EditView gets focus? I think overriding View.onFocusChanged() would be the way to go. If you want to track the detailed input into EditText, implement TextWatcher and use TextView.addTextChangedListener() to add it to the EditText instance.
精彩评论