EditText
An EditText object represents an input text field on the screen.
Example:
< EditText
android:id="@+id/editURLid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType = "text"
android:text="http://www."
android:editable = "true"
android:cursorVisible = "true"
android:singleLine = "true"
/>
You can declare a reference variable to the EditText in code.
You only need a reference variable if you need to refer to the EditText.
private EditText theURL;
You can get a reference to it by calling findViewById.
You usually do this in the onCreate method.
theURL = (EditText) findViewById(R.id.editURLid);
You can call methods in code using the reference variable.
theURL.setText("http://www.");
String theText = theURL.getText().toString();
theURL.setInputType(InputType.TYPE_NULL);
To Show:
EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
// import android.view.inputmethod.*;
// android.content.Context
To Hide:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
// use this to dismiss the keyboard after pressing Enter on the keyboard
// where editText is a variable referring to the EditText field
// do this in the onCreate method
editText = (EditText) findViewById(R.id.inputTextId);
editText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
// do something
// now dismiss the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
default:
break;
}
}
return false;
}
});