Android EditText setText and text input last time?

I am using EditText to enter some information and setText to EditText. But I have a problem when EditText is displayed, keyboard display too. when i enter text, it will add text first not after the text i set before!

this is my editText with keyboard

[keyboard input text here] example <- this example is my setText enter image description here

I need to customize the input text after my text as follows

example [here is the keyboard input text] enter image description here

Thanks for the help....

-1


source to share


5 answers


I think you need setSelection to move to the end of your text



EditText et = (EditText)findViewById(R.id.edittext1);
et.setSelection(et.getText().length());

      

+3


source


You have to set the selection to the end like this:

EditText editText = (EditText) findViewById(R.id.editText);
editText.setText("smb://");
editText.setSelection(editText.getText().length());

      



Note. If you need space, update setText

as follows:editText.setText("example ");

+1


source


for this you can add a textChanged listener to edit the text and you can add the default text to the index you want (I mean you override these methods to implement your logic)

   edittext.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
     editText.setText("<add your desired text here>");//for suppose here your text is "example" (length 7 )
     editText.setSelection(<your text length[here 7 for the text "example"]>);



    }
});

      

0


source


Set the default text in your edittext widget and in your main class by making it a text change listener and for convenience I have placed a print expression to see the entered text

package com.example.edittext;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
    String defaultText;
    EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.edittext);
        defaultText  = text.getText().toString();
        text.setSelection(defaultText.length());
        text.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //text.setText(defaultText+s);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                System.out.println(s.toString());


            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

      

0


source


You can use the method setSelection(int index)

on yours EditText

to move the cursor to the end EditText

.

So for example:

EditText et = (EditText)findViewById(R.id.editText);
et.setText("smb://");

// after you set the text, write this line:
et.setSelection(et.length()); 
// or et.setSelection(et.getText().toString().length());

      

This will set the cursor to EditText

the last position of the text in EditText

.

0


source







All Articles