How to add text to editext

2019-02-24 00:14发布

I've got a problem with populating an edittext. Using the following code i can set the text just fine, however what i am trying to do is add to the edittext. For example the following code displays a "1" in my edittext but if I press it again it just replaces the "1" with "1" and so on. What i need is it to display "1111" if I press it four times.

heres my code:

@Override
    public void onClick(View v) {
        switch (v.getId()) {
                    case R.id.button1:

                        Button txtnum = (Button) findViewById(R.id.button1);

                        Bundle bundle = new Bundle();
                        bundle.putString("number1", txtnum.getText().toString());
                        String title = bundle.getString("number1");
                        ((EditText) findViewById(R.id.editText1)).setText(title);



                        break;

Hope this makes sence. (I'm a noob) If anyone can help me with this I'd really appreciate it. thanks Steve

5条回答
何必那么认真
2楼-- · 2019-02-24 00:19

You'd need editText.setText(editText.getText() + "string");.

EditText et = (EditText) findViewById(R.id.editText1);
et.setText(et.getText() + title);
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-24 00:24

Set edit text to the previuos value plus the new value.

EditText et = (EditText) findViewById(R.id.editText1);
et.setText(et.GetText() + title);
查看更多
够拽才男人
4楼-- · 2019-02-24 00:27

Should be as simple as:

editText.setText("hello");

In your code:

EditText editText=(EditText)findViewById(R.id.x);
editText.setText("hello");
查看更多
家丑人穷心不美
5楼-- · 2019-02-24 00:33

try this code

String title = bundle.getString("number1");
EditText editText = (EditText) findViewById(R.id.editText1);
editText.append(title);

if you want to set the only new value use this

editText.setText(title);
查看更多
淡お忘
6楼-- · 2019-02-24 00:37
String title = bundle.getString("number1");
EditText editText = (EditText) findViewById(R.id.editText1);
editText.setText(editText.getText().toString() + title);
查看更多
登录 后发表回答