下面列出了android.view.inputmethod.InputMethodManager#toggleSoftInputFromWindow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Shows the soft keyboard
*/
public static void showKeyboard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
private void editSelectedTextEntity() {
if (!(currentEntityView instanceof TextPaintView) || editingText) {
return;
}
curtainView.setVisibility(View.VISIBLE);
final TextPaintView textPaintView = (TextPaintView) currentEntityView;
initialText = textPaintView.getText();
editingText = true;
editedTextPosition = textPaintView.getPosition();
editedTextRotation = textPaintView.getRotation();
editedTextScale = textPaintView.getScale();
textPaintView.setPosition(centerPositionForEntity());
textPaintView.setRotation(0.0f);
textPaintView.setScale(1.0f);
toolsView.setVisibility(GONE);
setTextDimVisibility(true, textPaintView);
textPaintView.beginEditing();
InputMethodManager inputMethodManager = (InputMethodManager) ApplicationLoader.applicationContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(textPaintView.getFocusedView().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
private void editSelectedTextEntity() {
if (!(currentEntityView instanceof TextPaintView) || editingText) {
return;
}
curtainView.setVisibility(View.VISIBLE);
final TextPaintView textPaintView = (TextPaintView) currentEntityView;
initialText = textPaintView.getText();
editingText = true;
editedTextPosition = textPaintView.getPosition();
editedTextRotation = textPaintView.getRotation();
editedTextScale = textPaintView.getScale();
textPaintView.setPosition(centerPositionForEntity());
textPaintView.setRotation(0.0f);
textPaintView.setScale(1.0f);
toolsView.setVisibility(GONE);
setTextDimVisibility(true, textPaintView);
textPaintView.beginEditing();
InputMethodManager inputMethodManager = (InputMethodManager) ApplicationLoader.applicationContext.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(textPaintView.getFocusedView().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.action_filter:
if (db.getList().size() != 0) {
actionDrawer();
}
return true;
case R.id.action_sort:
if (getPreferences(MODE_PRIVATE).getBoolean("isAlphabeticalOrder", false)) {
getPreferences(MODE_PRIVATE).edit().putBoolean("isAlphabeticalOrder", false).apply();
} else {
getPreferences(MODE_PRIVATE).edit().putBoolean("isAlphabeticalOrder", true).apply();
}
actionSort(true);
return true;
case R.id.action_changePassword:
changePassword();
return true;
case R.id.action_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
case R.id.action_about:
AboutToast.getInstance().createAboutToast(getPackageManager(), getPackageName(), toast0, getApplicationContext());
return true;
case R.id.action_search:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInputFromWindow(listview.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
default:
return super.onOptionsItemSelected(item);
}
}
public static void enableSoftInput(@NonNull View view, boolean toEnable) {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
if (toEnable) {
imm.toggleSoftInputFromWindow(view.getWindowToken(), InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* Shows the soft keyboard
*/
public static void showKeyboard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.edit_container:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
findViewById(R.id.edit_container)
.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
break;
default:
break;
}
}
private void openSearchInternal(Boolean openKeyboard) {
this.mLogoView.setVisibility(View.GONE);
this.mSearchEditText.setVisibility(View.VISIBLE);
mSearchEditText.requestFocus();
this.mSuggestionListView.setVisibility(View.VISIBLE);
mSuggestionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
hideKeyboard();
NiboSearchSuggestionItem result = mSearchSuggestions.get(arg2);
if (mSearchListener != null) {
if (mSearchListener.onSuggestion(result)) {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
} else {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
}
});
String currentSearchText = getSearchText();
if (currentSearchText.length() > 0) {
buildSearchSuggestions(currentSearchText);
} else {
buildEmptySearchSuggestions();
}
if (mSearchListener != null)
mSearchListener.onSearchEditOpened();
if (getSearchText().length() > 0) {
showClearButton();
}
if (openKeyboard) {
if (showCustomKeyboard && mCustomKeyboardView != null) { // Show custom keyboard
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
// Enable cursor, but still prevent default keyboard from showing up
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mSearchEditText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
case MotionEvent.ACTION_MOVE:
layout = ((EditText) v).getLayout();
x = event.getX() + mSearchEditText.getScrollX();
offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch point was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
}
return true;
}
};
mSearchEditText.setOnTouchListener(otl);
} else { // Show default keyboard
mSearchEditText.setOnTouchListener(null);
InputMethodManager inputMethodManager = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}
}
public static void showKeyboard(Context context, View view) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager == null) return;
inputMethodManager.toggleSoftInputFromWindow(view.getWindowToken(), InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
}
private void openSearchInternal(Boolean openKeyboard) {
this.mLogoView.setVisibility(View.GONE);
this.mSearchEditText.setVisibility(View.VISIBLE);
mSearchEditText.requestFocus();
this.mSuggestionListView.setVisibility(View.VISIBLE);
mSuggestionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
hideKeyboard();
SearchItem result = mSearchSuggestions.get(arg2);
if (mSearchListener != null) {
if (mSearchListener.onSuggestion(result)) {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
} else {
setSearchString(result.getValue(), true);
fromEditingToSearch(true, false);
}
}
});
String currentSearchText = getSearchText();
if (currentSearchText.length() > 0) {
buildSearchSuggestions(currentSearchText);
} else {
buildEmptySearchSuggestions();
}
if (mSearchListener != null)
mSearchListener.onSearchEditOpened();
if (getSearchText().length() > 0) {
showClearButton();
}
if (openKeyboard) {
if(showCustomKeyboard && mCustomKeyboardView != null) { // Show custom keyboard
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
// Enable cursor, but still prevent default keyboard from showing up
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCustomKeyboardView.setVisibility(View.VISIBLE);
mCustomKeyboardView.setEnabled(true);
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mSearchEditText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
case MotionEvent.ACTION_MOVE:
layout = ((EditText) v).getLayout();
x = event.getX() + mSearchEditText.getScrollX();
offset = layout.getOffsetForHorizontal(0, x);
if (offset > 0)
if (x > layout.getLineMax(0))
mSearchEditText.setSelection(offset); // Touch point was at the end of the text
else
mSearchEditText.setSelection(offset - 1);
break;
}
return true;
}
};
mSearchEditText.setOnTouchListener(otl);
} else { // Show default keyboard
mSearchEditText.setOnTouchListener(null);
InputMethodManager inputMethodManager = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}
}
private void openSearch(Boolean openKeyboard) {
if(animateDrawerLogo){
this.materialMenu.animateState(IconState.ARROW);
this.drawerLogo.setVisibility(View.GONE);
}
this.logo.setVisibility(View.GONE);
this.search.setVisibility(View.VISIBLE);
search.requestFocus();
this.results.setVisibility(View.VISIBLE);
animate = true;
setAdapter(new SearchAdapter(context, resultList, search));
searchOpen = true;
results.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SearchResult result = resultList.get(arg2);
search(result, true);
}
});
if(initialResults != null){
setInitialResults();
}else{
updateResults();
}
if (listener != null)
listener.onSearchOpened();
if (getSearchText().length() > 0) {
micStateChanged(false);
mic.setImageDrawable(context.getResources().getDrawable(
R.drawable.ic_clear));
}
if (openKeyboard) {
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}