的setText不设置文字的EditText [英] setText doesn't set text to EditText

查看:118
本文介绍了的setText不设置文字的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我从未有过的近三年时间开发了Android ...的

I'm having a problem that I've never had before in almost three years of developing with Android...

我要拍张照片,图像被抽放后,的EditText S中的活动变得清晰。我在做什么使用设置了的EditText 的值字符串 的getText()的ToString()拍摄照片后恢复它们。

I want to take a picture and after the picture is taked, the EditTexts of the activity become clear. What I'm doing is set the values of the EditText to Strings using getText().toString() to restore them after taking the picture.

中的字符串是完全存储的数据,但是当我使用的setText ,这是行不通的......奇怪的是, setHint 作品!

The strings are stored perfectly with the data, but when I use setText, it doesn't work... The strange thing is that setHint works!

这怎么可能?

这里的code我使用:

Here's the code I'm using:

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

名称说明是全球性的 EditTexts tempSpotName tempSpotDesc 是全球性的字符串

name and description are global EditTexts and tempSpotName and tempSpotDesc are global Strings.

我怎样才能设置文本?

推荐答案

onActivityResult()不是当返回到一个活动叫最后一个方法。你可以刷新你的记忆<一href="http://developer.android.com/reference/android/app/Activity.html#onActivityResult%28int,%20int,%20android.content.Intent%29">the生命周期的文档。 :)

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

正如我们在评论中讨论,如果的setText()再打电话像 onResume()此方法将覆盖 onActivityResult任何文本集()

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

也是一样的片段,你需要在onViewStateRestored更新()方法(它添加在API 17)。

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

这篇关于的setText不设置文字的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆