2013-04-10 58 views
0

例如,我在嘗試在novo.charAt(0)處接收值1後嘗試設置togglebutton(LeftLightButton)狀態和文本時出現了一些問題。主要的想法是,我點擊按鈕,它使webview.loadUrl,如果頁面更改爲我所期望的切換按鈕狀態應該是,如果不是,它必須保持關閉,反之亦然。現在的方式是,如果網站沒有更新到我期望的值,則不會更改文本。Android:切換按鈕不更新

public class MainActivity extends Activity { 

private WebView webView; 
private ToggleButton LeftLightButton; 
private Thread webviewthread; 
private String baseURL; 

private boolean LeftLightButtonState; 

public void onCreate(Bundle savedInstanceState) { 

    final Context context = this; 
    baseURL = "http://192.168.1.4/i.php"; 

    LeftLightButton = (ToggleButton) findViewById(R.id.toggleButton1); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    runOnUiThread(new Runnable() { 
      public void run() 
      { 
       synchronized(this) { 
        webView = (WebView) findViewById(R.id.webView1); 

        webView.getSettings().setJavaScriptEnabled(true); 
        webView.addJavascriptInterface(context, "HTMLOUT"); 

        webView.setWebViewClient(new WebViewClient() { 
         @Override 
         public void onPageFinished(WebView view, String url) { 
          webView.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');"); 
         } 
        }); 

        webView.loadUrl(baseURL); 
       } 
      } 
     }); 

    showToast("Getting current status..."); 

} 

public void notify(final boolean state, final ToggleButton buttonName) { 

    Thread thread = new Thread() { 
     @Override 
     public void run() { 
      synchronized (this) { 
       try { 
        wait(1000); 
        final ToggleButton button = (ToggleButton) findViewById(R.id.toggleButton1); 
        if (!state && button.isChecked()) { 
         showToast("Couldnt turn OFF"); 
         // UI 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 

           button.setChecked(false); 
           button.setTextOff("OFF"); 
          } 
         }); 
         // end of UI 

        } 
        if (state && !button.isChecked()) { 
         showToast("Couldnt turn ON"); 
         // UI 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 

           button.setChecked(true); 
           button.setTextOn("ON"); 
          } 
         }); 
         // end of UI 
        } 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 


     } 
    }; 
    thread.start(); 

} 

public void showToast(final String toast) 
{ 
    runOnUiThread(new Runnable() { 
     public void run() 
     { 
      Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

public void onClick(View arg0) { 

    final int id = arg0.getId(); 
    switch (id) { 
    case R.id.toggleButton1: 
     if (LeftLightButtonState == true) { 
      webView.loadUrl(baseURL+"?L=0"); 
      notify(false, LeftLightButton); 
     } else { 
      webView.loadUrl(baseURL+"?L=1"); 
      notify(true, LeftLightButton); 
     } 
     break; 
    // even more buttons here 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void processHTML(String html) { // <html> <body> 1 0 0 0 </body> </html> 
    LeftLightButton = (ToggleButton) findViewById(R.id.toggleButton1); 
    String novo = android.text.Html.fromHtml(html).toString(); 
    System.out.println("RECEIVED: "+novo); 

    if (novo != null) { 
     if (novo.charAt(0) == '0') { 
      LeftLightButtonState = false;  
      LeftLightButton.setTextOff("OFF"); 
      LeftLightButton.setChecked(false); 

     } 
     if (novo.charAt(0) == '1') { 
      LeftLightButtonState = true; 
      LeftLightButton.setTextOn("ON"); 
      LeftLightButton.setChecked(true); 

     } 
     System.out.println("CHEGUEI"); 
    } 


} 


} 

回答

0

在xml文件本身中使用不同的字符串。然後根據狀態顯示適當的文字。你可以調用setChecked(true/false)來選擇或取消選擇切換按鈕。它在我的項目中工作正常。

android:textOff="@string/dontshowtraffic" 
    android:textOn="@string/showtraffic"