2012-04-19 209 views
1

我正在使用下面的代碼,並且我有一個未顯示的按鈕。我認爲它與「SetContentView」有關,因爲如果我刪除其中的一個,該按鈕將顯示出來。我不知道如何解決這個問題,讓所有東西都顯示出來?謝謝!沒有顯示Java按鈕?

import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 

import android.widget.Button; 
import android.widget.TextView; 

public class TestActivity extends Activity { 
/** Called when the activity is first created. 
* @return */ 



@Override 
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
    Button OffWifi = (Button)findViewById(R.id.offwifi); 
    OffWifi.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) {     
      WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE); 
      wifiManager.setWifiEnabled(false);   
      }   
     }); 



    TextView tv = new TextView(this);  
    TextView status = new TextView(this);     


    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration wc = new WifiConfiguration(); 
    wc.SSID = "\"Test\""; //IMP! This should be in Quotes!! 

    wc.hiddenSSID = true; 
    wc.status = WifiConfiguration.Status.ENABLED;  
    wc.priority = 10; 
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    wc.preSharedKey = "\"Password\""; 
    WifiManager wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE); 
    boolean res1 = wifiManag.setWifiEnabled(true); 
    int res = wifi.addNetwork(checkPreviousConfiguration(wc)); 
    Log.d("WifiPreference", "add Network returned " + res); 
    boolean es = wifi.saveConfiguration(); 
    Log.d("WifiPreference", "saveConfiguration returned " + es); 
    boolean b = wifi.enableNetwork(res, true);  
    Log.d("WifiPreference", "enableNetwork returned " + b); 

    tv.setText("You are now connected! " + 
      "Version 1.1"); 

    status.setText("The was an error connecting, please try again."); 

     //@Override 

    try { 

     Thread.sleep(5000); 

     ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

     if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ 
       //You are connected, do something online. 
       setContentView(tv); 

      }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {    
       //Not connected.   
       setContentView(status); 
      } 

    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 





} 

public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) { 
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();  
    for(WifiConfiguration config : configs) {   
     if(config.SSID.equals(wc.SSID)) return config;  
     }  
    return wc; 
    } 

} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<TextView 
android:id="@+id/tv" 
android:layout_width="246dp" 
android:layout_height="wrap_content" /> 


<Button 
android:id="@+id/offwifi" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Turn Wifi Off" /> 

</LinearLayout> 

回答

1

setContentView()不會將視圖添加到顯示中,它會替換它們。這就是爲什麼你的按鈕不在了。

在您的XML佈局中使用現有的電視,而不是創建一個新的電視,並且只調用一次setContentReview()。

像這樣:

super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
Button OffWifi = (Button)findViewById(R.id.offwifi); 
OffWifi.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) {     
     WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE); 
     wifiManager.setWifiEnabled(false);   
     }   
    }); 


// remove these lines 
// TextView tv = new TextView(this);  
// TextView status = new TextView(this);     

// add this line 
TextView tv= (TextView) findViewById(R.id.tv); 


WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
WifiConfiguration wc.... 

ect.ect. 

Log.d("WifiPreference", "enableNetwork returned " + b); 

// let's do this later 
//tv.setText("You are now connected! " + 
//  "Version 1.1"); 

//status.setText("The was an error connecting, please try again."); 

    //@Override 

try { 

    Thread.sleep(5000); 

    ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

    if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ 

      //You are connected, do something online. 

      // setting the ContentView replaces everything, so don't do that 
      //setContentView(tv); 

      tv.setText("You are now connected! " + 
       "Version 1.1"); 



     }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {    
      //Not connected.   


      //setContentView(status); 
      tv.setText("The was an error connecting, please try again."); 
     } 

} catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

}

+0

謝謝!那工作 – user1342164 2012-04-19 19:31:40

1

你開始你的活動,並設置其內容視圖的東西,至少有一個按鈕主XML文件。然後,創建TextViews的新實例。然後在OnCreate中,將內容視圖設置爲其中一個文本視圖。如果連接,視圖將被替換爲TextView,並且如果未連接,視圖將替換爲TextView。如果您刪除了其中一個額外設置的內容呼叫,您將看到該按鈕取決於您是否連接。

這種行爲可能是你想要的,或者你可能想要做的是將TextViews添加到佈局中,並以FindViewById的方式獲取按鈕上的句柄。然後,您可以避開try塊中的所有內容,因爲文本視圖已經被新文本更新了。你的代碼目前所做的是用文本視圖替換整個佈局。

+0

你能告訴我一個例子?我對java很陌生。謝謝 – user1342164 2012-04-19 17:58:33

1

你需要得到根對象在main.xml中的佈局文件,即

LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout1); 

,那麼你可以添加到佈局(它可能還沒有被命名linearlayout1)

layout.addView (status); 
+0

這是我的xml文件。那麼我要添加另一個線性佈局嗎? – user1342164 2012-04-19 18:43:30

+0

你只需要使用if-else語句來設置你的TextView文本。使用TextView tv =(TextView)findViewbyId(R.id.tv)創建一個新的TextView,然後在你的try塊中使用if -else,並在那裏設置文本。 – 2012-04-19 19:04:35