我正在尝试将图像(imageView)与其关联文本(textView)一起动画。 我遵循了我在这里得到的建议,并使用AsyncTask和UI线程处理程序来动态更改图像和文本。这应该是一个简单的代码,但问题是我只看到/看到最后一个图像和文本。在下面的代码的情况下,将只显示im2(第二个图像)和t2(第二个文本)。
有人可以说出问题是什么吗?
我的代码是这样的:
public class picshow extends Activity
{
private static Integer[] Imgid = {R.drawable.im1, R.drawable.im2};
private static Integer[] Strid = {R.string.t1, R.string.t2};
private Handler handToUI = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
myTask task = new myTask();
task.execute("nothing");
}
private class myTask extends AsyncTask<String, Void, String >
{
@Override
protected String doInBackground(String... params )
{
//Do noting and return a String. a preparation for future code
return params[0];
}
@Override
protected void onPostExecute( String result )
{
//send message to UI
for(int i = 0;i<Strid.length;i++)
{
final int si = Strid[i];
final int ii = Imgid[i];
try
{
Thread.sleep(5000);
handToUI.post(new Runnable()
{
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
public void run()
{
text.setText(si);
image.setImageResource(ii);
}
});
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
感谢 梨皮
答案 0 :(得分:0)
试试这个。我已经更正了你的代码
handToUI.post(new Runnable()
{
ImageView image = (ImageView) findViewById(R.id.picView);
TextView text = (TextView) findViewById(R.id.usertext);
public void run()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
text.setText(si);
image.setImageResource(ii);
}
});
我希望它对你有所帮助。