我正在实现AsyncTask的示例,并从此处的示例AsyncTask Android example开始。
但是,我要进行更改。 在我的GUI中,我想放置一个文本框,用户可以在其中键入内容,并且每隔10秒,该名称就会出现在标签上。
这样,如果用户输入了新值,它将显示在标签上,而不是现在显示的静态文本“ Executed”。
我的代码在下面。
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="10dip">
</ProgressBar>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Progress" >
</Button>
<TextView android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Replace"/>
<EditText
android:id="@+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn;
EditText textInput;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
textInput = findViewById(R.id.textInput);
// because we implement OnClickListener we only have to pass "this"
// (much easier)
btn.setOnClickListener(this);
}
public void onClick(View view) {
// detect the view that was "clicked"
switch (view.getId()) {
case R.id.button1:
new LongOperation().execute("");
break;
}
}
private class LongOperation extends AsyncTask<String, Void, String> {
String input;
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
input = textInput.getText().toString();
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText(input); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
我将把代码放在哪里听?我希望更改标签,例如每10秒检查一次文本框中是否有更改...
编辑
基于答案,我可以这样离开无限循环吗?如果需要,如何停止此AsyncTask?
private class LongOperation extends AsyncTask<String, String, String> {
String input;
@Override
protected String doInBackground(String... params) {
while (true)
try {
Thread.sleep(1000);
input = textInput.getText().toString();
publishProgress(input);
} catch (InterruptedException e) {
Thread.interrupted();
return "Executed";
}
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(String... values) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText(values[0]);
// update the UI with Data received from publishprogress
}
}
答案 0 :(得分:2)
您可以使用publishprogess
从后台线程更新UI。
将Void
更改为String
以传递字符串类型并更新progressUpdate
中的UI
private class LongOperation extends AsyncTask<String, String, String> {
// ^^^^^^
String input;
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
input = textInput.getText().toString();
publishProgress(input);
//^^^^^^^^^ pass the data to update UI
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText(input); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(String... values) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText(values[0]);
// update the UI with Data received from publishprogress
}
}
请注意:您可以将textView
的初始化oncreate
移到oncreate
之外的地方进行声明,以避免一次又一次地初始化,这是一个昂贵的过程
编辑:要停止循环,您可以使用
while(!isCancelled()){ // run as long as task is not cancelled
}
要取消任务,请先存储参考
AsynchTask task = new LongOperation().execute("");
task.cancel(true);