Java Code Examples for android.widget.EditText#setSelection()

The following examples show how to use android.widget.EditText#setSelection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PopupActivity.java    From ankihelper with GNU General Public License v3.0 6 votes vote down vote up
private void setupEditNoteDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(PopupActivity.this);
        LayoutInflater inflater = PopupActivity.this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.dialog_edit_note, null);
        dialogBuilder.setView(dialogView);

        final EditText edt = (EditText) dialogView.findViewById(R.id.edit_note);
        edt.setHorizontallyScrolling(false);
        edt.setMaxLines(4);
        edt.setText(mNoteEditedByUser);
        edt.setSelection(mNoteEditedByUser.length());
        dialogBuilder.setTitle(R.string.dialog_note);
        //dialogBuilder.setMessage("输入笔记");
        dialogBuilder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                mNoteEditedByUser = edt.getText().toString();
            }
        });
//                        dialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int whichButton) {
//                                //pass
//                            }
//                        });
        AlertDialog b = dialogBuilder.create();
        b.show();
    }
 
Example 2
Source File: AppRTCDemoActivity.java    From droidkit-webrtc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void showGetRoomUI() {
  final EditText roomInput = new EditText(this);
  roomInput.setText("https://apprtc.appspot.com/?r=");
  roomInput.setSelection(roomInput.getText().length());
  DialogInterface.OnClickListener listener =
      new DialogInterface.OnClickListener() {
        @Override public void onClick(DialogInterface dialog, int which) {
          abortUnless(which == DialogInterface.BUTTON_POSITIVE, "lolwat?");
          dialog.dismiss();
          connectToRoom(roomInput.getText().toString());
        }
      };
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder
      .setMessage("Enter room URL").setView(roomInput)
      .setPositiveButton("Go!", listener).show();
}
 
Example 3
Source File: ShowLogcatActivity.java    From FreezeYou with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    processSetTheme(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showlogcat_activity);
    processActionBar(getActionBar());

    EditText editText = findViewById(R.id.sla_log_editText);

    try {
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String log_line;
        String rn = System.getProperty("line.separator");
        while ((log_line = bufferedReader.readLine()) != null) {
            editText.setText(editText.getText().append(log_line).append(rn));
        }
        bufferedReader.close();
        process.destroy();
        editText.setSelection(editText.getText().length());

    } catch (IOException e) {
        e.printStackTrace();
        editText.setText(editText.getText().append(e.getLocalizedMessage()));
    }
}
 
Example 4
Source File: MeFragment.java    From AndroidPlusJava with GNU General Public License v3.0 6 votes vote down vote up
private void showEditUserNameDialog() {
    View content = LayoutInflater.from(getContext()).inflate(R.layout.dialog_edit, null);
    final EditText editText = content.findViewById(R.id.dialog_edit);
    editText.setText(mUser.getUsername());
    editText.setSelection(editText.getText().length());
    new AlertDialog.Builder(getContext())
            .setTitle(R.string.edit_user_name)
            .setNegativeButton(R.string.cancel, null)
            .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String updateUserName = editText.getText().toString().trim();
                    if (!mUser.getUsername().equals(updateUserName)) {
                        updateUserName(updateUserName);
                    }
                }
            })
            .setView(content)
            .show();
}
 
Example 5
Source File: EditTimerActivity.java    From ClockPlus with GNU General Public License v3.0 6 votes vote down vote up
@OnClick({ R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
            R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine })
    void onClick(TextView view) {
        if (mFocusGrabber.isFocused())
            return;
        EditText field = getFocusedField();
        int at = field.getSelectionStart();
        field.getText().replace(at, at + 1, view.getText());
        field.setSelection(at + 1);
//        updateStartButtonVisibility();
        if (field.getSelectionStart() == FIELD_LENGTH) {
            // At the end of the current field, so try to focus to the next field.
            // The search will return null if no view can be focused next.
            View next = field.focusSearch(View.FOCUS_RIGHT);
            if (next != null) {
                next.requestFocus();
                if (next instanceof EditText) {
                    // Should always start off at the beginning of the field
                    ((EditText) next).setSelection(0);
                }
            }
        }
    }
 
Example 6
Source File: NickSignActivity.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
private void initView() {
    mEd_sign = (EditText) findViewById(R.id.ed_sign);
    mLl_nickSign = (LinearLayout) findViewById(R.id.ll_nickSign);
    mTv_count = (TextView) findViewById(R.id.tv_count);
    mJmui_commit_btn = (Button) findViewById(R.id.jmui_commit_btn);

    if (getIntent().getStringExtra("group_name") != null) {
        mEd_sign.setText(getIntent().getStringExtra("group_name"));
    }
    if (getIntent().getStringExtra("group_desc") != null) {
        mEd_sign.setText(getIntent().getStringExtra("group_desc"));
    }
    if (getIntent().getStringExtra("old_nick") != null) {
        mEd_sign.setText(getIntent().getStringExtra("old_nick"));
    }
    if (getIntent().getStringExtra("old_sign") != null) {
        mEd_sign.setText(getIntent().getStringExtra("old_sign"));
    }

    mEd_sign.setSelection(mEd_sign.getText().length());

}
 
Example 7
Source File: CommentEditor.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
public final void formatSelectedText(EditText commentView, int what) {
	int start = commentView.getSelectionStart();
	int end = commentView.getSelectionEnd();
	if (start == -1 || end == -1) {
		return;
	}
	FormatResult result = formatSelectedText(commentView.getText(), what, start, end);
	if (result != null) {
		commentView.setSelection(result.start, result.end);
	}
}
 
Example 8
Source File: NoteEditActivity.java    From Notepad with Apache License 2.0 5 votes vote down vote up
@Override
protected void onStart() {
    super.onStart();

    // Set text in EditView
    if(external != null) {
        EditText noteContents = findViewById(R.id.editText1);
        noteContents.setText(external);
        noteContents.setSelection(external.length(), external.length());
        external = null;
    }
}
 
Example 9
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
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);

}
 
Example 10
Source File: CommentDialogFragment.java    From v2ex-daily-android with Apache License 2.0 5 votes vote down vote up
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mEditText = (EditText) getActivity().getLayoutInflater().inflate(R.layout.fragment_comment_dialog, null);
    if(getArguments().containsKey("comment_content")){
        mEditText.setText(getArguments().getString("comment_content"));
        mEditText.setSelection(mEditText.getText().length());
    }
    return new AlertDialog.Builder(getActivity())
            .setView(mEditText)
            .setPositiveButton(R.string.title_comment, this)
            .setCancelable(true)
            .setNegativeButton(R.string.title_cancel, null)
            .create();
}
 
Example 11
Source File: EditTextDialogBuilder.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("InflateParams")
public EditTextDialogBuilder(Context context, String text, String hint) {
    super(context);
    View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_edittext_builder, null);
    setView(view);
    mTextInputLayout = (TextInputLayout) view;
    mEditText = (EditText) view.findViewById(R.id.edit_text);
    mEditText.setText(text);
    mEditText.setSelection(mEditText.getText().length());
    mEditText.setOnEditorActionListener(this);
    mTextInputLayout.setHint(hint);
}
 
Example 12
Source File: CreateFileFragment.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 13
Source File: SoftKeyboardDemoScene.java    From scene with Apache License 2.0 5 votes vote down vote up
public static void focusAndShowInputMethod(EditText editText) {
    if (editText == null) {
        return;
    }
    editText.requestFocus();
    // In case the filter is not working, setText again.
    editText.setText(editText.getText());
    editText.setSelection(TextUtils.isEmpty(editText.getText()) ? 0 : editText.getText().length());
    ((InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText, 0);
}
 
Example 14
Source File: EditTextUtil.java    From AndroidBasicProject with MIT License 5 votes vote down vote up
/**
 * 设置文本,并将光标移动到文本末尾
 * @param editText
 * @param text
 */
public static void setText(EditText editText,String text){
    if(null != editText && !TextUtils.isEmpty(text)){
        editText.setText(text);
        editText.setSelection(text.length());
    }
}
 
Example 15
Source File: InputPanel.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
private void showInputMethod(EditText editTextMessage) {
    editTextMessage.requestFocus();
    //如果已经显示,则继续操作时不需要把光标定位到最后
    if (!isKeyboardShowed) {
        editTextMessage.setSelection(editTextMessage.getText().length());
        isKeyboardShowed = true;
    }

    InputMethodManager imm = (InputMethodManager) container.activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editTextMessage, 0);

    container.proxy.onInputPanelExpand();
}
 
Example 16
Source File: EditTextSelectionState.java    From quill with MIT License 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 17
Source File: DialogFactory.java    From Tok-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * rename dialog
 */
public static DialogView inputDialog(Activity activity, String title, final String originData,
    final InputCallBack inputCallBack) {
    View view = ViewUtil.inflateViewById(activity, R.layout.dialog_layout_input);
    final EditText inputEt = view.findViewById(R.id.id_input_et);
    if (!StringUtils.isEmpty(originData)) {
        inputEt.setText(originData);
        inputEt.setSelection(originData.length());
    }

    DialogView dialogView = new DialogView(activity, view, false);
    dialogView.setRightOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String data = inputEt.getText().toString();
            if (StringUtils.isEmpty(data)) {
                ToastUtils.show(R.string.input_msg);
                return;
            }
            if (data.equals(originData)) {
                ToastUtils.show(R.string.input_msg_same_to_origin);
                return;
            }
            if (inputCallBack != null) {
                inputCallBack.input(data);
            }
        }
    });
    dialogView.setTitle_(title);
    dialogView.show();
    return dialogView;
}
 
Example 18
Source File: Rename.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
public static AlertDialog getRenameDialog(final BaseActivity activity,
                                          final File_POJO file,
                                          final BroadcastReceiver broadcastReceiver) {

    Theme theme = Settings.getInstance(activity).getThemeInstance(activity);
    ContextThemeWrapper wrapper = new ContextThemeWrapper(activity, theme.getDialogThemeRes());

    @SuppressLint("InflateParams")
    View dialogLayout = LayoutInflater.from(wrapper)
            .inflate(R.layout.input_dialog_layout, null, false);

    final EditText editText = dialogLayout.findViewById(R.id.edit_text);
    String name = file.getName();
    int index = name.lastIndexOf(".");
    //String fileExtension = name.substring(index, name.length());
    name = name.substring(0, index != -1 ? index : name.length());
    editText.setText(name);
    editText.setSelection(name.length());

    AlertDialog dialog = new AlertDialog.Builder(wrapper)
            .setTitle(R.string.rename)
            .setView(dialogLayout)
            .setPositiveButton(R.string.rename, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    final String newFileName = editText.getText().toString();

                    if (broadcastReceiver != null) {
                        activity.registerLocalBroadcastReceiver(broadcastReceiver);
                    }

                    final File_POJO[] files = new File_POJO[]{file};
                    Intent intent = FileOperation.getDefaultIntent(activity, FileOperation.RENAME, files)
                                    .putExtra(FileOperation.NEW_FILE_NAME, newFileName);
                    activity.startService(intent);
                }
            })
            .setNegativeButton(R.string.cancel, null)
            .create();
    //noinspection ConstantConditions
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return dialog;
}
 
Example 19
Source File: ChangeText.java    From Noyze with Apache License 2.0 4 votes vote down vote up
private void setSelection(EditText editText, int start, int end) {
    if (start >= 0 && end >= 0) {
        editText.setSelection(start, end);
    }
}
 
Example 20
Source File: UiUtils.java    From EosCommander with MIT License 4 votes vote down vote up
public static void setTextAndMoveCursorToEnd(EditText editText, CharSequence data ){
    if ( null != data ) {
        editText.setText(data);
        editText.setSelection(data.length());
    }
}