edittext 中的值在获得焦点之前不会显示。安卓
我正在构建一个根据外部数据获取其布局的应用程序。根据indata,布局被分成不同的块。这些块在视图翻转器的帮助下显示。在每个块中,暂时有一个或多个“textview”和“edittext”。在翻转器的第一页上,所有数据都按其应有的方式显示在文本视图和编辑文本中。但在视图翻转器的其他页面上,直到编辑文本获得焦点后,编辑文本中的值才会显示。我不知道为什么。所以要明确一点。所有编辑文本的值实际上都在那里,但直到编辑文本获得焦点后才会显示。在我运行该应用程序的所有设备(模拟器、HTC Desire、HTC Wildfire)上,问题都是相同的。有谁知道如何解决这个问题?
这是生成布局的类:
public class ModuleLayout extends LinearLayout {
public ModuleLayout(Context context, ArrayList<BaseModuleItem> itemList) {
super(context);
TableLayout tempTable = new TableLayout(context);
for (int i = 0; i < itemList.size(); i++)
{
TableRow tempRow = new TableRow(context);
TextView tempTextView = new TextView(context);
tempTextView.setText(itemList.get(i).getName());
EditText tempEditText = new EditText(context);
tempEditText.setText(itemList.get(i).getItemValue());
tempRow.addView(tempTextView);
tempRow.addView(tempEditText);
tempTable.addView(tempRow);
}
tempTable.setColumnStretchable(1, true);
this.addView(tempTable);
}
}
这是实际问题的图片。
左图显示了所有值。右图位于视图翻转器中的第二个位置,并且不显示编辑文本中的值,但具有焦点的第一个图片除外。我还应该提到,在编辑文本获得焦点后,即使我切换到视图翻转器中的其他视图然后再返回,它也会继续显示该值。
I'm building an app that get its layout based on external data. The layout is split into different blocks depending on the indata. These blocks are displayed with the help of a viewflipper. In each block there is, for the time being, one or more "textview" and "edittext". On the first page of the flipper all data is showing as it should in the textviews and edittexts. But on the other pages in the viewflipper the value in the edittexts is not showing until the edittext get focused. I have no idea why. So to be clear. The values for all edittexts is actually there, but doesn't show until the edittext get focused. The problem is the same on all devices I have runned the app on (emulator, HTC Desire, HTC Wildfire). Does anyone know how to fix this problem?
Here is the class that generate the layout:
public class ModuleLayout extends LinearLayout {
public ModuleLayout(Context context, ArrayList<BaseModuleItem> itemList) {
super(context);
TableLayout tempTable = new TableLayout(context);
for (int i = 0; i < itemList.size(); i++)
{
TableRow tempRow = new TableRow(context);
TextView tempTextView = new TextView(context);
tempTextView.setText(itemList.get(i).getName());
EditText tempEditText = new EditText(context);
tempEditText.setText(itemList.get(i).getItemValue());
tempRow.addView(tempTextView);
tempRow.addView(tempEditText);
tempTable.addView(tempRow);
}
tempTable.setColumnStretchable(1, true);
this.addView(tempTable);
}
}
Here is a picture of the problem in action.
The left picture displays all its values fine. The right picture is on the second position in the viewflipper and does not display the values in the edittexts, with the exception for the first that have focus. I should also mention that after an edittext has gotten focus it continues to display the value even if I fling to other views in the viewflipper and then back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从此处显示的布局来看,该布局不在 UI 线程上,这意味着无论您更改什么,在 UI 线程(也称为主线程)更新之前您都不会看到任何内容。 UI/主线程是您所有活动运行的地方,因此很容易认为这些内容会自动更新(我假设您之前完成的大部分工作也在活动中的某个位置)。 onFocusChanged 事件(在 edittext 中单击)将通过调用自身的 invalidate() 来更新 UI 线程。如果需要,您可以通过调用 EditText.invalidate() 来强制它更新,但通常最好让它在创建后自然更新。在没有看到你的其余代码的情况下,这是我能为你做的最好的事情,我希望这有帮助:)
That Layout from how it appears here is not on the UI thread meaning no matter what you change you won't see anything until the UI thread (also known as the Main Thread) updates. The UI/Main thread is what all your activities run through so it's easy to think that stuff just updates automatically (since most of the previous work you have done is also somewhere within the Activity, I am assuming). The onFocusChanged event (clicking in the edittext) will update the UI thread by calling invalidate() on itself. You can force it to update by calling EditText.invalidate() if you need to, but it's usually best to have it update naturally after creation. Without seeing the rest of your code this is the best I can do for you, I hope this helps :)
没有看到你的完整代码,我做了一些猜测建议,但是你尝试过 textView.invalidate() (如果从 GUI 线程调用)或 textView.postInvalidate() (如果从另一个线程调用)吗?
Having not seeing your full code I'm making a bit of a guess-suggestion but have you tried textView.invalidate() (if calling from GUI thread) or textView.postInvalidate() (if calling from another thread)?