2012-10-24 235 views
0

我遇到了麻煩,看起來應該很簡單。我有一個框架佈局與2個按鈕(一個在另一個頂部自然)。當我點擊頂部按鈕時,它會自動將我帶到一個網站,並且它下面的按鈕會將其替換爲可見的按鈕。我想設置一個自動刷新,以便在幾秒鐘後,原本位於頂部的按鈕再次成爲頂部。感謝您給予的任何幫助!這裏是Java,並與我創造的自動刷新嘗試:Android自動刷新按鈕

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); 

回答

1

的問題是,你刷新可運行僅第一個按鈕註冊一個onClickListener並調用每10秒鐘本身,你應該只註冊onClickListener一次在Runnable之外,並且只調用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); 
     } 

    });