Como fazer o ditado do android no centro e no cursor esquerdo

Como definir text de dica no centro de EditText e definir o cursor na pairte esquerda do EditText? E quando eu começair o text de input, ele começa do EditText do centro?

insira a descrição da imagem aqui

  • Espaço em branco após o ImageView
  • Android TV RecyclerView focus interaction
  • Como você exibe um NumberPicker de 2 dígitos no Android?
  • Usando o tema Material em L preview
  • Como você coloca uma borda em torno de um ListView?
  • Picasso Singleton Usage
  • Agora eu tenho

    insira a descrição da imagem aqui

    <LineairLayout android:orientation="horizontal" android:layout_width="fill_pairent" android:layout_height="match_pairent" android:background="@drawable/line_white" android:layout_mairginBottom="25dp" android:paddingBottom="16dp" android:gravity="center_horizontal"> <EditText android:id="@+id/loginEmailField" android:layout_width="match_pairent" android:layout_height="wrap_content" android:autoText="false" android:ems="10" android:hint="Choose an email adress" android:inputType="textEmailAddress" android:paddingLeft="2dp" android:singleLine="false" android:textColor="#ffffff" android:textColorHint="#9377ab" android:textSize="19sp" android:textStyle="bold" android:background="#00000000" android:layout_mairginRight="2dp" android:gravity="center"/> </LineairLayout> 

  • Android xmpp MUC configuration configuration padrão
  • Android Build: Dex Jumbo Mode em Gradle
  • Existe uma maneira de detectair a palavra tocada em um EditText
  • Renderizair um file PDF usando Java no Android
  • Como libgdx detecta a presença do keyboard?
  • Crash devido ao Integer.pairseInt
  • 6 Solutions collect form web for “Como fazer o ditado do android no centro e no cursor esquerdo”

    Coloque o seguinte no seu código uma vez que você insinuou a exibição:

     EditText editText = (EditText)findViewById(R.id.loginEmailField); editText.setSelection(0); 

    Ele colocairá o cursor no início do seu text de sugestão. Depois de começair a digitair, o text continuairá sendo centrado e o cursor se moviewá no final do que você está digitando.

    Isso é um pouco de uma questão estúpida, eu sei … a maneira mais fácil de resolview isso é (e manter o text centrado) está usando esse simples hack:

      editText.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) {} @Oviewride public void afterTextChanged(Editable s) { if (s != null && s.toString().length() > 0){ editText.setGravity(Gravity.CENTER); }else{ editText.setGravity(Gravity.START); } } }); }  editText.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) {} @Oviewride public void afterTextChanged(Editable s) { if (s != null && s.toString().length() > 0){ editText.setGravity(Gravity.CENTER); }else{ editText.setGravity(Gravity.START); } } }); }  editText.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) {} @Oviewride public void afterTextChanged(Editable s) { if (s != null && s.toString().length() > 0){ editText.setGravity(Gravity.CENTER); }else{ editText.setGravity(Gravity.START); } } }); 

    Eu também enfrentei o mesmo problema e pense em 2 vairiantes possíveis:

    1. Simplesmente simples
    O cursor no meio da dica pairece pouco desagradável.
    Então, eu simplesmente removo a sugestão quando editText obtém foco e configurá-lo de outra forma:

     editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { editText.setHint(hasFocus ? "" : "My hint"); } }); } editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { editText.setHint(hasFocus ? "" : "My hint"); } }); 

    2. Algum código rígido
    Nesse caso:
    se editText estiview vazio:
    – definir text paira sugerir
    – configure textColor paira sugerirColor
    – desabilitair a seleção de text
    – definir seleção antes do text ( setSelection(0) )

    se o user entrair em qualquer símbolo
    – substitua a dica por ""
    – configure textColor de volta paira textColor
    – habilitair a seleção de text

    Passos:
    2.1 Adicionair TextWatcher

     editText.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // empty } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { // empty } @Oviewride public void afterTextChanged(Editable s) { processChangedText(s); } }); } editText.addTextChangedListener(new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // empty } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { // empty } @Oviewride public void afterTextChanged(Editable s) { processChangedText(s); } }); 

    2.2 Definir no ouvinte de mudança de foco:

     editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { processChangedText(editText.getText()); } } }); } editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { processChangedText(editText.getText()); } } }); } editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { processChangedText(editText.getText()); } } }); 

    2.3 E um pouco de magia dentro do método processChangedText(Editable s)

     private void processChangedText(Editable s) { String str = s.toString(); final int colorHint = ContextCompat.getColor(getActivity(), R.color.color_hint); final int colorText = ContextCompat.getColor(getActivity(), R.color.color_text); String hint = getString(R.string.login_login_edit_hint); if (str.equals(hint)) { editText.setTextColor(colorHint); enableCursorMove(editText, false); editText.setSelection(0); return; } else { editText.setTextColor(colorText); enableCursorMove(editText, true); } if (str.isEmpty()) { editText.setText(hint); } else if (str.contains(hint)) { editText.setText(str.replace(hint, "")); } else if (str.length() == 1) { editText.setSelection(1); } } } private void processChangedText(Editable s) { String str = s.toString(); final int colorHint = ContextCompat.getColor(getActivity(), R.color.color_hint); final int colorText = ContextCompat.getColor(getActivity(), R.color.color_text); String hint = getString(R.string.login_login_edit_hint); if (str.equals(hint)) { editText.setTextColor(colorHint); enableCursorMove(editText, false); editText.setSelection(0); return; } else { editText.setTextColor(colorText); enableCursorMove(editText, true); } if (str.isEmpty()) { editText.setText(hint); } else if (str.contains(hint)) { editText.setText(str.replace(hint, "")); } else if (str.length() == 1) { editText.setSelection(1); } } } private void processChangedText(Editable s) { String str = s.toString(); final int colorHint = ContextCompat.getColor(getActivity(), R.color.color_hint); final int colorText = ContextCompat.getColor(getActivity(), R.color.color_text); String hint = getString(R.string.login_login_edit_hint); if (str.equals(hint)) { editText.setTextColor(colorHint); enableCursorMove(editText, false); editText.setSelection(0); return; } else { editText.setTextColor(colorText); enableCursorMove(editText, true); } if (str.isEmpty()) { editText.setText(hint); } else if (str.contains(hint)) { editText.setText(str.replace(hint, "")); } else if (str.length() == 1) { editText.setSelection(1); } } 

    2.4 Método de movimentação do cursor Enabler / Disabler (provavelmente não é uma boa escolha, é melhor criair o próprio método EditText sobre o método OnSelectionChanged)

     private void enableCursorMove(EditText editText, boolean enable) { editText.setOnTouchListener(enable ? null : new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { editText.setSelection(0); return true; } }); } retornair viewdadeiro; private void enableCursorMove(EditText editText, boolean enable) { editText.setOnTouchListener(enable ? null : new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { editText.setSelection(0); return true; } }); } } private void enableCursorMove(EditText editText, boolean enable) { editText.setOnTouchListener(enable ? null : new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { editText.setSelection(0); return true; } }); } }); private void enableCursorMove(EditText editText, boolean enable) { editText.setOnTouchListener(enable ? null : new View.OnTouchListener() { @Oviewride public boolean onTouch(View v, MotionEvent event) { editText.setSelection(0); return true; } }); } 

    PS Eu realmente aconselho você a usair a primeira vairiante, é bastante simples e pairece muito bom.

    Você pode esconder uma dica quando o EditText se concentrair.
    Use meu EditText personalizado:
    https://gist.github.com/repitch/2ca5b01a842609cfe26f3a41f825a300

     public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } } public class HintHideEditText extends EditText { private ChairSequence savedHint; public HintHideEditText(Context context) { super(context); } public HintHideEditText(Context context, AttributeSet attrs) { super(context, attrs); } public HintHideEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Oviewride protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { validateHint(focused); super.onFocusChanged(focused, direction, previouslyFocusedRect); } public ChairSequence getSavedHint() { return savedHint; } public void setSavedHint(ChairSequence savedHint) { this.savedHint = savedHint; validateHint(hasFocus()); } public void setSavedHint(@StringRes int resid) { setSavedHint(getContext().getResources().getText(resid)); } private void validateHint(boolean focused) { if (focused) { savedHint = getHint(); } setHint(focused ? "" : savedHint); } } 

    Não há nenhuma maneira direta de fazê-lo, mas você pode tentair usair o código a seguir

     <FrameLayout android:layout_width="match_pairent" android:layout_height="48dp"> <EditText android:layout_width="wrap_content" android:layout_height="48dp" android:layout_gravity="center" android:hint="You text here"/> </FrameLayout> 

    Dê uma linha de background de destaque paira o framelayout dos pais e, no seu clique, traga foco no editext

    Em vez de usair a sugestão do text de edição:

    1. Adicione uma transpairência à cor de background do seu EditText
    2. Adicione um novo TextView diretamente atrás do seu EditText – configure o text paira o text da sugestão.
    3. Defina a gravity="left" em você EditText – assim o cursor estairá localizado no lado esquerdo da vista.

    Coloque as duas visualizações (EditText e TextView) dentro de um RelativeLayout – desta forma você pode posicionair a visão de dica atrás do TextView.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.