我是android新手。我刚刚使用Aynctask创建了一个简单的应用程序。基本上,它使用Asynctask迭代循环并在文本框中显示迭代编号。我创建了一个edittext和一个textview,bot显示了一些奇怪的文本。我尝试创建一个吐司,并且toast正确显示相同的文本。这是源代码:
package com.jeannius.me;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
int[] color1 = {0xFFe48701, 0xFFa5bc4e, 0xFF1b95d9, 0xFFcaca9e, 0xFF6693b0, 0xFFf05e27, 0xFF86d1e4,
0xFFe4f9a0, 0xFFffd512, 0xFF75b000, 0xFF0662b0, 0xFFede8c6, 0xFFcc3300, 0xFFd1dfe7,
0xFF52d4ca, 0xFFc5e05d, 0xFFe7c174, 0xFFfff797, 0xFFc5f68f, 0xFFbdf1e6, 0xFF9e987d,
0xFFeb988d, 0xFF91c9e5, 0xFF93dc4a, 0xFFffb900, 0xFF9ebbcd, 0xFF009797, 0xFF0db2c2};
TextView yyr;
EditText ud;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yyr = (TextView)findViewById(R.id.mytext);
ud = (EditText)findViewById(R.id.edit);
Button myb =(Button)findViewById(R.id.mybut);
yyr.setTextColor(0xFFE48701);
ud.setRawInputType(InputType.TYPE_CLASS_TEXT);
myb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new mySyncTask().execute();
}
});
}
class mySyncTask extends AsyncTask< Void, Integer, Void>{
String red;
@Override
protected Void doInBackground(Void... unused) {
for(Integer j=0; j<5; j++){
red = String.valueOf(j);
publishProgress(j);
SystemClock.sleep(3000);}
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), red.toString(), Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... values) {
yyr.setText(String.valueOf(values));
ud.setText(values.toString(), TextView.BufferType.EDITABLE);
Toast.makeText(getApplicationContext(), red.toString(), Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
values是一个数组。像这样使用它:String.valueOf( values[0] )