setText不会将文本设置为EditText

KrL*_*ler 6 android settext android-edittext

我有一个问题,这是我用Android开发近三年来从未有过的问题...

我想拍照,拍完照片后,EditText活动的s变得清晰.我在做什么设置的值EditText,以Strings使用getText().toString()拍照后恢复它们.

字符串与数据完美地存储在一起,但是当我使用时setText,它不起作用......奇怪的是它setHint有效!

怎么会这样?

这是我正在使用的代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Image captured and saved to fileUri specified in the Intent

                grabImage(imgView);

                for (int u = 0; u <= 2; u++)
                {
                    if (savedImgs[u].equals(""))
                    {
                        imgs = u + 1;
                        savedImgs[u] = photo.toString();
                        break;
                    }
                }

                /*Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); ---> It is a small bitmap, for icons...
                imgView.setImageBitmap(thumbnail);
                imgView.setVisibility(View.VISIBLE);*/

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                Toast.makeText(this, "Image couldn't be taken. Try again later.", Toast.LENGTH_LONG).show();
            }
        }

        if (!tempSpotName.equals("") || !tempSpotDesc.equals("")) {

            name.setText(tempSpotName);
            description.setText(tempSpotDesc);
        }
    }
Run Code Online (Sandbox Code Playgroud)

name并且description是全球性的EditTexts,tempSpotName并且tempSpotDesc是全球性的Strings.

我该如何设置文字?

Sam*_*Sam 15

onActivityResult()不是返回Activity时调用的最后一个方法.您可以在文档中刷新对生命周期的记忆.:)

正如我们在评论中所讨论的那样,如果setText()再次使用这样的方法调用onResume()将覆盖任何设置的文本onActivityResult().

对于Fragments也是如此,您需要在onViewStateRestored()方法(在API 17中添加)中进行更新.


Jay*_*aya 10

有些时候,改变edittextonactivity结果是行不通的。我也面临同样的问题

而不是设置

edittext.settext("yourtext");
Run Code Online (Sandbox Code Playgroud)

更改为跟随 onactivityresult

edittext.post(new Runnable(){
edittext.settext("yourtext");
});
Run Code Online (Sandbox Code Playgroud)

它对我有用。