Android EditText setText and input text in last?

2019-09-20 00:05发布

I use EditText to input some information, and I setText in EditText.
But I have a problem, when EditText display, the keyboard display too. when I input text it will add text in first not after text that I set before!

this is my editText with keyboard

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

I need to set up input text after my text like this

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

Thanks for help ....

5条回答
放我归山
2楼-- · 2019-09-20 00:27

for that you can add a textChanged listener to edit text and you can add your default text at your desired index.(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"]>);



    }
});
查看更多
beautiful°
3楼-- · 2019-09-20 00:29

You should set the selection to the end as follows:

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

Note: If you need a space, then update your setText as follows: editText.setText("example ");

查看更多
\"骚年 ilove
4楼-- · 2019-09-20 00:30

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

EditText et = (EditText)findViewById(R.id.edittext1);
et.setSelection(et.getText().length());
查看更多
叼着烟拽天下
5楼-- · 2019-09-20 00:41

You can use the setSelection(int index) method on your EditText to move the cursor to the end of the 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());

That will set the cursor in the EditText to be at the last position of the text in the EditText.

查看更多
唯我独甜
6楼-- · 2019-09-20 00:45

Set a default text in your edittext widget and in your main class, impletement its text change listener and for the sake of convenience i have put a print statement to see the text entered

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;
    }

}
查看更多
登录 后发表回答