eclipse 从 Android 函数更新 Textview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4282001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Textview from function Android
提问by seba123neo
someone can tell me how to update a control Textview Android from a function? I have searched deep into the internet and see many people who ask the same question, I tested threads but could not work, someone has a simple working example of doing this? for example to call a function (which runs several times in a loop) and the function writes in the TextView, but the problem is that until the function is not finished running, it shows me the text.
有人可以告诉我如何从函数更新控件 Textview Android?我在互联网上进行了深入搜索,看到很多人问同样的问题,我测试了线程但无法工作,有人有一个简单的工作示例吗?例如调用一个函数(它在循环中运行多次)并且该函数写入TextView,但问题是直到该函数未完成运行,它才会向我显示文本。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
while(condition) //here freezes the UI and the text in textview only shows when the loop ends
{
HardWork();
}
}
public void HardWork() {
txtProgreso.append("Test Line" + "\n\n");
};
Thanks in advance.
提前致谢。
回答by Labeeb Panampullan
If i understood you correctly
Use AsyncTask here, So you can update textview in onProgressUpdate method
如果我理解正确,
请在此处使用 AsyncTask,这样您就可以在 onProgressUpdate 方法中更新 textview
private class SomeTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
while(condition) {
//do more
publishProgress("Some text value to you textview");
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
txtProgreso.append(values[0]);
}
}
回答by rwat
I think what you're trying to ask is "how do I force my TextView to show its updated contents without returning control to the runloop?" (ie, returning out of your function).
我认为您要问的是“如何强制我的 TextView 显示其更新的内容而不将控制权返回给 runloop?” (即,返回您的功能)。
If so, I'm afraid you're out of luck, such a thing is impossible to do with Android's UI model. The nearest thing to this that is possible is to keep track of your loop state, and set a timer that will call a function for you to update the TextView.
如果是这样,恐怕你就倒霉了,这种事情用Android的UI模型是不可能做到的。最接近的可能是跟踪您的循环状态,并设置一个计时器,该计时器将为您调用一个函数来更新 TextView。
Here's an example. Suppose you want to put the text "this is a test" into a TextView, one character at a time:
这是一个例子。假设您想将文本“this is a test”放入一个 TextView 中,一次一个字符:
private Handler mHandler = new Handler(); private String desiredText = new String("This is a test"); private Runnable mUpdateTextView = new Runnable() { public void run() { TextView textView = (TextView)findViewById(R.id.myTextView); int lengthSoFar = textView.getText().length(); if (lengthSoFar < desiredText.length()) { mTextView.setText(desiredText.substring(0, lengthSoFar + 1)); mHandler.postDelayed(100, mUpdateTextView); } } }; protected void onStart() { mHandler.postDelayed(100, mUpdateTextView); }
I think some of my object visibility is incorrect here, and I don't have an Android environment handy to test on, but you get the general idea. Rather than a Handler you could also use a Timer, but it's a bit more heavyweight so it depends how often you want to be updating the UI.
我认为我的一些对象可见性在这里是不正确的,而且我没有可以方便地测试的 Android 环境,但是您已经大致了解了。除了 Handler,您还可以使用 Timer,但它的重量更大,因此这取决于您希望更新 UI 的频率。
回答by dlongest
You get the textview by doing the following.
您可以通过执行以下操作来获取文本视图。
TextView myTextView = (TextView)findViewById(R.id.textViewsId);
Then afterwards, you can change the TextView using the functions. For example, you can set the text using the following code.
然后,您可以使用这些函数更改 TextView。例如,您可以使用以下代码设置文本。
myTextView.setText("New text example");
Are there any other updates to the control you are trying to do?
您正在尝试对控件进行任何其他更新吗?