本文整理匯總了Java中android.widget.EditText.setSelection方法的典型用法代碼示例。如果您正苦於以下問題:Java EditText.setSelection方法的具體用法?Java EditText.setSelection怎麽用?Java EditText.setSelection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.widget.EditText
的用法示例。
在下文中一共展示了EditText.setSelection方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: insertAtCursor
import android.widget.EditText; //導入方法依賴的package包/類
public static void insertAtCursor(@NonNull EditText editText, @NonNull String text) {
String oriContent = editText.getText().toString();
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
Logger.e(start, end);
if (start >= 0 && end > 0 && start != end) {
editText.setText(editText.getText().replace(start, end, text));
} else {
int index = editText.getSelectionStart() >= 0 ? editText.getSelectionStart() : 0;
Logger.e(start, end, index);
StringBuilder builder = new StringBuilder(oriContent);
builder.insert(index, text);
editText.setText(builder.toString());
editText.setSelection(index + text.length());
}
}
示例2: afterTextChanged
import android.widget.EditText; //導入方法依賴的package包/類
@Override
public void afterTextChanged(Editable editable) {
EditText editText = editTextWeakReference.get();
if (editText == null) return;
String s = editable.toString();
editText.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
BigDecimal parsed = new BigDecimal(cleanString)
.setScale(2, BigDecimal.ROUND_FLOOR)
.divide(new BigDecimal(100), BigDecimal.ROUND_FLOOR);
String decimalValue = decimalFormat.format(parsed);
editText.setText(decimalValue);
editText.setSelection(decimalValue.length());
editText.addTextChangedListener(this);
}
示例3: onApiError
import android.widget.EditText; //導入方法依賴的package包/類
@Override
public void onApiError(String error, boolean isEmailError) {
stopWaiting();
EditText errorView = mPasswordView;
TextView errorMsgView = mPasswordErrorView;
if (isEmailError) {
errorView = mEmailView;
errorMsgView = mEmailErrorView;
}
errorView.setSelection(errorView.getText().length());
KeyboardUtils.focusAndShowKeyboard(getActivity(), errorView);
errorMsgView.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
errorMsgView.setText(Html.fromHtml(error, Html.FROM_HTML_MODE_LEGACY));
} else {
errorMsgView.setText(Html.fromHtml(error));
}
// show the help tip, and let it stay there; no need to hide it again
mLoginHelpTipView.setVisibility(View.VISIBLE);
}
示例4: addSpace
import android.widget.EditText; //導入方法依賴的package包/類
/**
* 添加間隔
*
* @param ed EditText
* @param maxLength 有效位數 比如 電話11位
* @param location 要打間隔的位置
*/
private void addSpace(EditText ed, int maxLength, int... location) {
Editable s = ed.getText();
String val = s.toString();
String valRep = val.replace(" ", "");
StringBuilder sb = new StringBuilder(valRep);
for (int m = 0; m < location.length; m++) {
if (sb.length() > location[m] + m) {
sb.insert(location[m] + m, " ");
}
}
if (sb.length() > maxLength + location.length) {
String _s = sb.toString().substring(0, maxLength + location.length);
ed.setText(_s);
ed.setSelection(_s.length());
} else {
int k = mContent.getSelectionEnd();
ed.setText(sb);
ed.setSelection(isSelection ? k : sb.length());
}
}
示例5: addCode
import android.widget.EditText; //導入方法依賴的package包/類
public static void addCode(@NonNull EditText editText) {
try {
String source = editText.getText().toString();
int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();
String substring = source.substring(selectionStart, selectionEnd);
String result;
if (hasNewLine(source, selectionStart))
result = "```\n" + substring + "\n```\n";
else
result = "\n```\n" + substring + "\n```\n";
editText.getText().replace(selectionStart, selectionEnd, result);
editText.setSelection(result.length() + selectionStart - 5);
} catch (Exception e) {
e.printStackTrace();
}
}
示例6: showCustomEndpointDialog
import android.widget.EditText; //導入方法依賴的package包/類
private void showCustomEndpointDialog(final int originalSelection, String defaultUrl) {
View view = LayoutInflater.from(app).inflate(R.layout.debug_drawer_network_endpoint, null);
final EditText url = findById(view, R.id.debug_drawer_network_endpoint_url);
url.setText(defaultUrl);
url.setSelection(url.length());
new AlertDialog.Builder(getContext()) //
.setTitle("Set Network Endpoint")
.setView(view)
.setNegativeButton("Cancel", (dialog, i) -> {
endpointView.setSelection(originalSelection);
dialog.cancel();
})
.setPositiveButton("Use", (dialog, i) -> {
String theUrl = url.getText().toString();
if (!Strings.isBlank(theUrl)) {
setEndpointAndRelaunch(theUrl);
} else {
endpointView.setSelection(originalSelection);
}
})
.setOnCancelListener((dialogInterface) -> {
endpointView.setSelection(originalSelection);
})
.show();
}
示例7: insert2Input
import android.widget.EditText; //導入方法依賴的package包/類
void insert2Input(String strToInsert) {
EditText txtInput = (EditText)findViewById(R.id.edtSmartMathInput);
int nSelectionStart = txtInput.getSelectionStart();
int nSelectionEnd = txtInput.getSelectionEnd();
if (nSelectionStart < 0 || nSelectionEnd < 0
|| nSelectionStart > txtInput.getText().length()
|| nSelectionEnd > txtInput.getText().length()
|| nSelectionStart > nSelectionEnd) { // invalid selection
txtInput.setText(strToInsert);
txtInput.setSelection(txtInput.getText().length());
} else { // valid selection, insert the text
txtInput.getText().replace(nSelectionStart, nSelectionEnd, strToInsert);
txtInput.setSelection(nSelectionStart + strToInsert.length());
}
}
示例8: addHeader
import android.widget.EditText; //導入方法依賴的package包/類
public static void addHeader(@NonNull EditText editText, int level) {
String source = editText.getText().toString();
int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();
StringBuilder result = new StringBuilder();
String substring = source.substring(selectionStart, selectionEnd);
if (!hasNewLine(source, selectionStart))
result.append("\n");
IntStream.range(0, level).forEach(integer -> result.append("#"));
result.append(" ").append(substring);
editText.getText().replace(selectionStart, selectionEnd, result.toString());
editText.setSelection(selectionStart + result.length());
}
示例9: requestInputMethodIfShow
import android.widget.EditText; //導入方法依賴的package包/類
public static void requestInputMethodIfShow(final EditText view) {
final InputMethodManager imm = (InputMethodManager) MyApplication.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
view.requestFocus();
view.setSelection(view.getText().length(), view.getText().length());
}
}
示例10: addBold
import android.widget.EditText; //導入方法依賴的package包/類
public static void addBold(@NonNull EditText editText) {
String source = editText.getText().toString();
int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();
String substring = source.substring(selectionStart, selectionEnd);
String result = "**" + substring + "** ";
editText.getText().replace(selectionStart, selectionEnd, result);
editText.setSelection(result.length() + selectionStart - 3);
}
示例11: onCreateDialog
import android.widget.EditText; //導入方法依賴的package包/類
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
final View view = dialogInflater.inflate(R.layout.dialog_create_dir, null, false);
final EditText text1 = (EditText) view.findViewById(android.R.id.text1);
Utils.tintWidget(text1);
String title = getArguments().getString(EXTRA_DISPLAY_NAME);
if(!TextUtils.isEmpty(title)) {
text1.setText(title);
text1.setSelection(title.length());
}
builder.setTitle(R.string.menu_create_file);
builder.setView(view);
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String displayName = text1.getText().toString();
final String mimeType = getArguments().getString(EXTRA_MIME_TYPE);
String extension = FileUtils.getExtFromFilename(displayName);
final DocumentsActivity activity = (DocumentsActivity) getActivity();
final DocumentInfo cwd = activity.getCurrentDirectory();
new CreateFileTask(activity, cwd,
TextUtils.isEmpty(extension) ? mimeType : extension, displayName).executeOnExecutor(
ProviderExecutor.forAuthority(cwd.authority));
}
});
builder.setNegativeButton(android.R.string.cancel, null);
return builder.create();
}
示例12: UserDictionaryAddWordContents
import android.widget.EditText; //導入方法依賴的package包/類
UserDictionaryAddWordContents(final View view, final Bundle args) {
mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text);
mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut);
if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) {
mShortcutEditText.setVisibility(View.GONE);
view.findViewById(R.id.user_dictionary_add_shortcut_label).setVisibility(View.GONE);
}
final String word = args.getString(EXTRA_WORD);
if (null != word) {
mWordEditText.setText(word);
// Use getText in case the edit text modified the text we set. This happens when
// it's too long to be edited.
mWordEditText.setSelection(mWordEditText.getText().length());
}
final String shortcut;
if (UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) {
shortcut = args.getString(EXTRA_SHORTCUT);
if (null != shortcut && null != mShortcutEditText) {
mShortcutEditText.setText(shortcut);
}
mOldShortcut = args.getString(EXTRA_SHORTCUT);
} else {
shortcut = null;
mOldShortcut = null;
}
mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT
mOldWord = args.getString(EXTRA_WORD);
updateLocale(args.getString(EXTRA_LOCALE));
}
示例13: onCreate
import android.widget.EditText; //導入方法依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_bind_device_success);
AndroidBug54971Workaround.assistActivity(findViewById(android.R.id.content));
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 20) {
getWindow().setStatusBarColor(getResources().getColor(R.color.bar_color));
}
llBack = (LinearLayout) findViewById(R.id.ll_back);
llBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
TextView btnSure = (TextView) findViewById(R.id.tvEditSure);
btnSure.setVisibility(View.INVISIBLE);
TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
tvTitle.setText(R.string.find_devices);
coreID = getIntent().getStringExtra("coreID");
Log.d("XLight", "coreID:" + coreID);
EditText et = (EditText) findViewById(R.id.etControllerName);
et.setSelection(et.getText().length());
}
示例14: focusAndRestoreSelectionState
import android.widget.EditText; //導入方法依賴的package包/類
/**
* 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);
}
}
示例15: showNewNetworkProxyDialog
import android.widget.EditText; //導入方法依賴的package包/類
private void showNewNetworkProxyDialog(final ProxyAdapter proxyAdapter) {
final int originalSelection = networkProxyAddress.isSet() ? ProxyAdapter.PROXY : ProxyAdapter.NONE;
View view = LayoutInflater.from(app).inflate(R.layout.debug_drawer_network_proxy, null);
final EditText hostView = findById(view, R.id.debug_drawer_network_proxy_host);
if (networkProxyAddress.isSet()) {
String host = networkProxyAddress.get().getHostName();
hostView.setText(host); // Set the current host.
hostView.setSelection(0, host.length()); // Pre-select it for editing.
// Show the keyboard. Post this to the next frame when the dialog has been attached.
hostView.post(() -> Keyboards.showKeyboard(hostView));
}
new AlertDialog.Builder(getContext()) //
.setTitle("Set Network Proxy")
.setView(view)
.setNegativeButton("Cancel", (dialog, i) -> {
networkProxyView.setSelection(originalSelection);
dialog.cancel();
})
.setPositiveButton("Use", (dialog, i) -> {
String in = hostView.getText().toString();
InetSocketAddress address = InetSocketAddressPreferenceAdapter.parse(in);
if (address != null) {
networkProxyAddress.set(address);
// Force a restart to re-initialize the app with the new proxy.
ProcessPhoenix.triggerRebirth(getContext());
} else {
networkProxyView.setSelection(originalSelection);
}
})
.setOnCancelListener(dialogInterface -> networkProxyView.setSelection(originalSelection))
.show();
}