下面列出了android.widget.EditText#requestFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void onBackPress(EditText editText) {
int selection = editText.getSelectionStart();
if (selection == 0) {
int viewIndex = allLayout.indexOfChild(editText);
View v = allLayout.getChildAt(viewIndex - 1);
if (v != null) {
if (v instanceof EditText) {
if ((int) v.getTag() != 1) {
String s1 = editText.getText().toString();
EditText q = (EditText) v;
String s2 = q.getText().toString();
allLayout.removeView(editText);
q.setText(s1 + s2);
q.requestFocus();
q.setSelection(s2.length(), s2.length());
lastEditText = q;
}
} else if (v instanceof ImageView) {
allLayout.removeView(v);
}
}
}
}
protected void onPrepareDialog(int id, Dialog dialog) {
//AlertDialog aDialog = (AlertDialog) dialog;
switch (id) {
case DIALOG_COORD_PROPS_ID:
try {
EditText e1 = (EditText) dialog
.findViewById(R.id.mycoords_name);
e1.requestFocus();
DBHelper dbHelper = new DBHelper(SlideTabsActivity.this);
RepoCoordsFragment fragment = (RepoCoordsFragment) getSupportFragmentManager().findFragmentByTag(DBHelper.getFragmentTag(R.id.viewpager, 0));
e1.setText(dbHelper.getMyccordName(fragment.actionCoordsId));
dbHelper.close();
e1.selectAll();
} catch (Exception e) {
Log.d("gps", "EXCEPTION! " + e.toString() + " Message:" + e.getMessage());
}
break;
default:
break;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
state = GameState.GAME;
super.onCreate(savedInstanceState);
setContentView(R.layout.math_game_activity);
resultInput = (EditText) findViewById(R.id.result);
resultInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(resultInput.getWindowToken(), 0);
resultInput.setEnabled(false);
resultInput.setFocusable(false);
resultInput.setClickable(false);
String codeValue = resultInput.getText().toString();
gameDataRef.setValue(codeValue);
return true;
}
});
resultInput.requestFocus();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.shaky_toolbar);
EditText messageEditText = (EditText) view.findViewById(R.id.shaky_form_message);
ImageView attachmentImageView = (ImageView) view.findViewById(R.id.shaky_form_attachment);
Uri screenshotUri = getArguments().getParcelable(KEY_SCREENSHOT_URI);
int sendIconResource = getArguments().getInt(KEY_MENU);
String title = getArguments().getString(KEY_TITLE);
toolbar.setTitle(title);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
toolbar.setNavigationOnClickListener(createNavigationClickListener());
toolbar.inflateMenu(sendIconResource);
toolbar.setOnMenuItemClickListener(createMenuClickListener(messageEditText));
String hint = getArguments().getString(KEY_HINT);
messageEditText.setHint(hint);
messageEditText.requestFocus();
attachmentImageView.setImageURI(screenshotUri);
attachmentImageView.setOnClickListener(createNavigationClickListener());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new CheesesAdapter();
final ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(mAdapter);
final ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.search_view);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
final EditText editText = (EditText) actionBar.getCustomView();
editText.addTextChangedListener(mTextWatcher);
editText.requestFocus();
}
public static void initFocus(EditText editText) {
editToEnd(editText);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
editText.findFocus();
}
private void showKeyboard(final EditText ettext) {
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
}
, 200);
}
@Override
public void onClick() {
final EditText editText = (EditText) getView().findViewById(R.id.text_entry);
editText.requestFocus();
editText.setText(String.format("%s: %s", getObject(), editText.getText()));
editText.setSelection(editText.getText().length());
}
protected static boolean validateThemeDisplayText(Context context, EditText editDisplay, boolean grabFocus )
{
boolean isValid = true;
editDisplay.setError(null);
if (editDisplay.getText().toString().trim().isEmpty())
{
isValid = false; // display text is empty
editDisplay.setError(context.getString(R.string.edittheme_error_displaytext));
if (grabFocus)
editDisplay.requestFocus();
}
return isValid;
}
public void setFocusToList(View view, int position) {
TableLayout tableLayout = (TableLayout) view;
int count = tableLayout.getChildCount();
if (tableLayout.getChildCount() > 0) {
TableRow tableRow = (TableRow) tableLayout.getChildAt(position == POSITION_START ? 0 : count - 1);
if (tableRow != null) {
EditText editText = (EditText) tableRow.findViewById(R.id.txtText);
if (editText.requestFocus()) {
editText.setSelection(editText.getText().length());
}
}
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_write_comment, container, false);
setHasOptionsMenu(true);
edit = (EditText) layout.findViewById(R.id.edit_text);
edit.requestFocus();
if (savedInstanceState == null && comment != null)
edit.setText(comment.getEditableContent());
return layout;
}
/**
* 为给定的编辑器开启软键盘
*
* @param editText 给定的编辑器
*/
public static void openSoftKeyboard(Context context, EditText editText) {
editText.requestFocus();
InputMethodManager inputMethodManager
= (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText,
InputMethodManager.SHOW_IMPLICIT);
}
/**
* Focus the associated EditText and restore its selection state.
*/
public void focusAndRestoreSelectionState() {
EditText focusedEditView = this.getEditText();
int start = this.getSelectionStart();
int end = this.getSelectionEnd();
final int len = focusedEditView.getText().length();
focusedEditView.requestFocus();
// cursor pos is == length, when it's at the very end
if (start <= end && start >= 0 && start <= len && end >= 0 && end <= len) {
focusedEditView.setSelection(start, end);
} else if (end >= 0 && end <= len) {
focusedEditView.setSelection(end);
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final EditText paramEditText = (EditText) view.findViewById(R.id.mEditText);
paramEditText.requestFocus();
paramEditText.post(new Runnable() {
@Override
public void run() {
((InputMethodManager) getActivity().getSystemService(Context
.INPUT_METHOD_SERVICE)).showSoftInput(paramEditText, 0);
}
});
}
public static void onActive(Context context, EditText et) {
if (et == null)
return;
et.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, 0);
}
/**
* Requsets focus on current pin view and opens keyboard if forceKeyboard is enabled.
*
* @return the current focused pin view. It can be used to open softkeyboard manually.
*/
public View requestPinEntryFocus() {
int currentTag = Math.max(0, getIndexOfCurrentFocus());
EditText currentEditText = editTextList.get(currentTag);
if (currentEditText != null) {
currentEditText.requestFocus();
}
openKeyboard();
return currentEditText;
}
private void startEditTagsMode() {
ImageButton editButton = getActivity().findViewById(R.id.edit_tags_btn);
editButton.setImageResource(R.drawable.ic_edit_anim);
((Animatable) editButton.getDrawable()).start();
((DrawerLayout) ((MainActivity) getActivity()).drawer).setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mRefreshLayout.setEnabled(false);
getActivity().findViewById(R.id.refresh_fab).setEnabled(false);
((RefreshIcon) getActivity().findViewById(R.id.refresh_fab)).hide();
((Toolbar) getActivity().findViewById(R.id.toolbar)).getMenu().clear();
ViewSwitcher viewSwitcher = getActivity().findViewById(R.id.switcher);
EditText songTV = getActivity().findViewById(R.id.song);
TextView artistTV = getActivity().findViewById(R.id.artist);
EditText newLyrics = getActivity().findViewById(R.id.edit_lyrics);
newLyrics.setTypeface(LyricsTextFactory.FontCache.get("light", getActivity()));
newLyrics.setText(Html.fromHtml(TextUtils.isEmpty(mLyrics.getText()) ? "" : mLyrics.getText()), TextView.BufferType.EDITABLE);
viewSwitcher.setVisibility(View.GONE);
newLyrics.setVisibility(View.VISIBLE);
songTV.setInputType(InputType.TYPE_CLASS_TEXT);
artistTV.setInputType(InputType.TYPE_CLASS_TEXT);
songTV.setBackgroundResource(R.drawable.abc_textfield_search_material);
artistTV.setBackgroundResource(R.drawable.abc_textfield_search_material);
if (songTV.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.SHOW_IMPLICIT);
}
}
public static void hideKeyboard(Context ct,EditText v)
{
if(ct!=null && v!=null) {
v.requestFocus();
((InputMethodManager) ct.getSystemService(ct.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* A method to take care of all the initialisations.
*
* @param context
* @param attrs
* @param defStyleAttr
*/
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
this.removeAllViews();
mPinHeight *= DENSITY;
mPinWidth *= DENSITY;
mSplitWidth *= DENSITY;
setWillNotDraw(false);
initAttributes(context, attrs, defStyleAttr);
params = new LayoutParams(mPinWidth, mPinHeight);
setOrientation(HORIZONTAL);
createEditTexts();
super.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
boolean focused = false;
for (EditText editText : editTextList) {
if (editText.length() == 0) {
editText.requestFocus();
openKeyboard();
focused = true;
break;
}
}
if (!focused && editTextList.size() > 0) { // Focus the last view
editTextList.get(editTextList.size() - 1).requestFocus();
}
if (mClickListener != null) {
mClickListener.onClick(Pinview.this);
}
}
});
// Bring up the keyboard
final View firstEditText = editTextList.get(0);
if (firstEditText != null)
firstEditText.postDelayed(new Runnable() {
@Override
public void run() {
openKeyboard();
}
}, 200);
updateEnabledState();
}
public static void showKeyboard(EditText mEtSearch, Context context) {
mEtSearch.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}