开发者

Android: Refresh view problem

开发者 https://www.devze.com 2023-01-15 17:38 出处:网络
I am having a problem trying to refresh a View in an Android application. I have a button that have a image and what I need to do is to change the image when someone clicked the button.

I am having a problem trying to refresh a View in an Android application. I have a button that have a image and what I need to do is to change the image when someone clicked the button.

Where is the problem? The image don't refresh until the activity finished proccessing the code. Any idea how I can refresh the image as soon as It execute the instruction

buttton1.setBackgroundDrawable(getResources().getDrawable(R.draw开发者_如何学Cable.f1));


Have you considered using the xml side and have the drawables as selectors as then the selectors will get chosen by the particular key/touch event to display the correct graphic..


Try running your method that does your processing from a thread.

ficha1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
        Button bot = (Button) findViewById(R.id.boton1); 
        bot.setBackgroundDrawable(getResources().getDrawable(R.drawable.f2)); 
        //ficha.setText(fichas.get("boton1").toString()); 
        new Thread(
            new Runnable() {
                public void run() {
                    controlJugada(fichas.get("boton1").toString(), bot);
                }
            }
        ).start();
    } 
});


I solved a similar problem by putting the offending code in a post delayed handler with a zero delay.

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // code slowing things down here
        }
    }, 0);


The first thing you do in the onclick listner is change the backgrount of the button

0

精彩评论

暂无评论...
验证码 换一张
取 消