我遇到了近三年的 Android 开发中从未遇到过的问题...
我想拍照,拍照后, Activity 的 EditText
变得清晰。我正在做的是使用 getText().toString()
将 EditText
的值设置为 Strings
以在拍照后恢复它们.
字符串与数据完美地存储在一起,但是当我使用 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);
}
}
name
和 description
是全局的 EditTexts
和 tempSpotName
和 tempSpotDesc
是全局的字符串
。
如何设置文字?
最佳答案
onActivityResult()
不是返回 Activity 时调用的最后一个方法。你可以刷新你对the Life Cycle的内存在文档中。 :)
正如我们在评论中讨论的那样,如果您在 onResume()
等方法中再次调用 setText()
,这将覆盖 onActivityResult() 中设置的任何文本
。
Fragments也是一样,需要在onViewStateRestored()方法(API 17新增)中进行更新。
关于android - setText 不会将文本设置为 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13387318/