0

我遇到了一些看起来应该很简单的问题。我有一个带有 2 个按钮的框架布局(一个自然位于另一个按钮之上)。当我单击顶部按钮时,它会自动将我带到一个网站,并且它下方的按钮将其替换为可见的。我想设置一个自动刷新,以便几秒钟后,原来在顶部的按钮再次变为顶部。感谢您提供的任何帮助!这是Java,我尝试创建自动刷新:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
    final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
    final WebView webview1= (WebView)this.findViewById(R.id.webView1);
    final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);

    final Handler handler = new Handler();
    Refresh = new Runnable() {
        public void run() {

        bJava1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                webview1.loadUrl("http://www.google.com");
                if(sound.isPlaying()){
                    bJava1.setVisibility(ImageButton.VISIBLE);
                    bJava2.setVisibility(ImageButton.GONE);
                }else {
                    sound.start();
                    bJava1.setVisibility(ImageButton.GONE);
                    bJava2.setVisibility(ImageButton.VISIBLE);
                }
            }

        });
        handler.postDelayed(Refresh, 10000);
    }
};
handler.post(Refresh);
4

1 回答 1

1

问题是,您的 Refresh runnable 仅在第一个按钮上注册一个 onClickListener 并每 10 秒调用一次,您应该只在 Runnable 之外注册一次 onClickListener 并且只在 Refresh.run() 方法中调用 if 块:

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final ImageButton bJava1= (ImageButton)findViewById(R.id.button1);
final ImageButton bJava2 = (ImageButton)findViewById(R.id.button2);
final WebView webview1= (WebView)this.findViewById(R.id.webView1);
final MediaPlayer sound= MediaPlayer.create(Youtube.this, R.raw.soundclip1);
final Handler handler = new Handler();
Refresh = new Runnable() {
    public void run() {
       if(sound.isPlaying()){
                bJava1.setVisibility(ImageButton.VISIBLE);
                bJava2.setVisibility(ImageButton.GONE);
            }else {
                sound.start();
                bJava1.setVisibility(ImageButton.GONE);
                bJava2.setVisibility(ImageButton.VISIBLE);
            }
     }
};
bJava1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            webview1.loadUrl("http://www.google.com");
            handler.postDelayed(Refresh, 10000);
        }

    });
于 2012-10-24T16:10:38.850 回答