当前位置: 首页>>代码示例>>Java>>正文


Java EditText.setSelection方法代码示例

本文整理汇总了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());
    }
}
 
开发者ID:duyp,项目名称:mvvm-template,代码行数:17,代码来源:MarkDownProvider.java

示例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);
}
 
开发者ID:ViniciusSossela,项目名称:meuboleto,代码行数:17,代码来源:NumberTextWatcher.java

示例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);
}
 
开发者ID:TryGhost,项目名称:Ghost-Android,代码行数:21,代码来源:PasswordAuthFragment.java

示例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());
    }
}
 
开发者ID:abook23,项目名称:godlibrary,代码行数:28,代码来源:EditTextWatcher.java

示例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();
    }
}
 
开发者ID:duyp,项目名称:mvvm-template,代码行数:20,代码来源:MarkDownProvider.java

示例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();
}
 
开发者ID:rogues-dev,项目名称:superglue,代码行数:27,代码来源:DebugView.java

示例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());
	}
}
 
开发者ID:woshiwpa,项目名称:SmartMath,代码行数:16,代码来源:ActivitySmartCalc.java

示例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());

}
 
开发者ID:duyp,项目名称:mvvm-template,代码行数:15,代码来源:MarkDownProvider.java

示例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());
    }
}
 
开发者ID:mmjang,项目名称:quiz_helper,代码行数:8,代码来源:ViewUtil.java

示例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);

}
 
开发者ID:duyp,项目名称:mvvm-template,代码行数:11,代码来源:MarkDownProvider.java

示例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();
}
 
开发者ID:kranthi0987,项目名称:easyfilemanager,代码行数:37,代码来源:CreateFileFragment.java

示例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));
}
 
开发者ID:sergeychilingaryan,项目名称:AOSP-Kayboard-7.1.2,代码行数:30,代码来源:UserDictionaryAddWordContents.java

示例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());
}
 
开发者ID:Datatellit,项目名称:xlight_android_native,代码行数:30,代码来源:BindDeviceSuccessActivity.java

示例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);
    }
}
 
开发者ID:TryGhost,项目名称:Ghost-Android,代码行数:17,代码来源:EditTextSelectionState.java

示例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();
}
 
开发者ID:rogues-dev,项目名称:superglue,代码行数:37,代码来源:DebugView.java


注:本文中的android.widget.EditText.setSelection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。