setText doesn't set text to EditText

故事扮演 提交于 2019-12-04 06:44:18
Sam

onActivityResult() is not the last method called when returning to an Activity. You can refresh your memory of the Life Cycle in the docs. :)

As we discussed in the comments, if you call setText() again in methods like onResume() this will override any text set in onActivityResult().

The same goes for Fragments, you need to make updates in onViewStateRestored() method (which was added in API 17).

Jaya

Some times changing edittext in onactivity result is not working. I too faced the same problem

instead of setting

edittext.settext("yourtext");

change to following in onactivityresult

edittext.post(new Runnable(){
edittext.settext("yourtext");
});

It worked for me.

Goot

First of all you have to debug this.

There is a class called TextWatcher. This will be called every time your Textbox.Text will change. So this is easier to debug and handle the problem. Url: http://developer.android.com/reference/android/text/TextWatcher.html

Example for implementation:

name.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

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

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        doSomething();



    } 

});

Good luck :)

You can force a EditText.SetText("blablabla..."); inside your OnActivity Result in 3 EASY steps:

  1. Reload your layout into your Activity
  2. Rebind your EditText
  3. use SetText as usual.

In this sample code, I pass a URL string with and intent and write it into a TextView:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{       
    if (resultCode == RESULT_OK) 
    {
        QRdata= data.getStringExtra("QRURL");

        if (QRdata.length()>0)
        {
                            //Step1
            setContentView(R.layout.activity_confirmpackage); 
                            //Step2
            TextView qrtxt=(TextView)this.findViewById(R.id.qrurl); 
                            //Setp 3,Voilà!
            qrtxt.setText(QRdata.toString()); 
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!