0

为什么这不会改变我的 TextView 的文本?我有一个TextView我尝试从 view.post 方法修改的文本。我在这里做错了什么?

mTextView = (TextView)findViewById(R.id.text_view);

new Thread(new Runnable() {
    @Override
    public void run() {
        mTextView.post(new Runnable(){
            @Override
            public void run() {
                mTextView.setText("A change in text, thank you very much");
            }
        });
    }
}).start();

这在 Activity 的 onCreate() 方法中运行。但是,如果我定义一个处理程序并将 Runnable 传递给处理程序,则 TextView 会被修改。

编辑:我在这里的代码基于示例:

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap =
                    loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
        }
    }).start();
}

http://developer.android.com/guide/components/processes-and-threads.html

编辑2:现在我完全糊涂了。当我从附加到 Activity 中的按钮的 onClickListener 运行相同的代码时,TextView 的文本确实被操纵了。为什么在方法中没有发生这种情况onCreate

4

3 回答 3

2

为了更新活动中的文本视图,您必须使用 runOnUiThread

runOnUiThread(new Runnable() {
    @Override 
    public void run() { 
        mTextView.setText("A change in text, thank you very much"); 
    }
}); 

您可以使用 With 使用 AsycTask 在 Imageview 中加载您的图像

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) {
        try { 
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);

       // you can also update your textview here 

    } 

} 

并在您的 Onclick 上调用此任务

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // pass your url and your imageview 
                // eg :http://example.com/image.png
                // imageView=(your ImageView)
                   new ImageLoadTask(url, imageView).execute();
            }
        });
于 2015-05-02T12:59:09.053 回答
0

为什么你在另一个线程中运行你的代码?所有 UI 操作仅在 UI 线程中有效。尝试仅执行以下代码

mTextView = (TextView)findViewById(R.id.text_view);
mTextView.post(new Runnable() {
    @Override
    public void run() {
        mTextView.setText("A change in text, thank you very much");
    }
});
于 2015-05-02T12:49:14.570 回答
0

不跑就跑new Thread(new Runnable()

     mTextView.post(new Runnable(){

         @Override
         public void run() {

             // anything you want
         }
     });
于 2015-05-02T13:03:02.270 回答