我试图添加一个ProgressDialog,同时使用AsyncTask将文本从一个编辑文本传输到另一个编辑文本。有人能帮我处理密码吗?
你能解释一下为什么我们需要使用AsyncTask吗?实际上,我是否需要使用处理程序来更新进度?
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Text.." >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="Transfer" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="114dp"
android:ems="10"
android:hint="Received Text.." />
</RelativeLayout>MainActivity.java
package com.example.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
EditText et1, et2;
String sent;
ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(MainActivity.this);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
sent = et1.getText().toString();
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
TransferText tt = new TransferText();
tt.execute(sent);
}
});
}
class TransferText extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
progress = new ProgressDialog(MainActivity.this);
progress.setTitle("Transferring...");
progress.setMessage("Please wait.");
progress.setCancelable(false);
progress.setIndeterminate(true);
progress.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = sent;
final int time = 100;
Thread t = new Thread() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Transferring",
Toast.LENGTH_SHORT).show();
int i = 0;
while (i < time) {
try {
sleep(50);
i += 5;
progress.setProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
return result;
}
@Override
protected void onPostExecute(String result) {
et2.setText(result);
}
}
}我希望转会发生在后台,同时显示前面的进度栏。当进度条达到100%时,传输应该完成。
发布于 2014-04-09 07:57:32
您绝对不应该使用ASyncTask将文本从一个EditText移动到另一个EditText。
在此之后:
sent = et1.getText().toString();这样做:
et2.setText(sent);或者简单地说:
et2.setText(et1.getText().toString());发布于 2014-04-09 08:01:33
您不需要使用异步类将文本从一个编辑文本传输到另一个编辑文本。如果你想同时发送文本,你可以使用文本处理程序。
https://stackoverflow.com/questions/22956118
复制相似问题