2013-04-22 79 views
-2

我成功地從我的android傳遞字符串到計算機(通過WiFi)使用兩個簡單的button.but我的問題是我想用單個單選按鈕(作爲切換),而不是兩個簡單的按鈕。這裏是我的代碼使用單選按鈕(電臺組)而不是兩個簡單的按鈕

  import java.io.IOException; 
      import java.io.PrintWriter; 
      import java.net.Socket; 
      import java.net.UnknownHostException; 

      import android.os.Bundle; 
      import android.app.Activity; 
      import android.view.View; 
      import android.view.View.OnClickListener; 
      import android.widget.Button; 


      public class MainActivity extends Activity { 


      private Socket client; 
      private PrintWriter printwriter; 

      private Button button; 
      private Button button1; 
      private String messsage; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 


      button = (Button) findViewById(R.id.button1); 
      button1 = (Button) findViewById(R.id.button2); 
      button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      messsage = "TV ON" ; //etMsg.getText().toString(); 
      //etMsg.setText(""); 
      // port = Integer.parseInt(etPort.getText().toString()); 

      new Thread(new Runnable() 
      { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        try 
        { 
         // client = new Socket(etIP.getText().toString(), port); 
         client = new Socket("1.2.3.4",2000); 

         printwriter = new PrintWriter(client.getOutputStream(),true); 
         printwriter.write(messsage); 
         printwriter.flush(); 
         printwriter.close(); 
         client.close(); 
        } 

        catch (UnknownHostException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        }  
       } 
      }).start(); 


     } 
    }); 



button1.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     messsage = "TV OFF" ; //etMsg.getText().toString(); 
     //etMsg.setText(""); 
     // port = Integer.parseInt(etPort.getText().toString()); 

     new Thread(new Runnable() 
     { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       try 
       { 
        // client = new Socket(etIP.getText().toString(), port); 
        client = new Socket("1.2.3.4",2000); 

        printwriter = new PrintWriter(client.getOutputStream(),true); 
        printwriter.write(messsage); 
        printwriter.flush(); 
        printwriter.close(); 
        client.close(); 
       } 

       catch (UnknownHostException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }  
      } 
     }).start(); 


    } 
}); 
     } 

    } 
+1

我真的不明白你的問題,是什麼問題? – Padrus 2013-04-22 09:55:24

+0

http://www.mkyong.com/android/android-radio-buttons-example/ – Triode 2013-04-22 09:55:43

+0

對不起,我的英語。實際上我想用單個單選按鈕而不是兩個簡單的按鈕,如我的代碼所示。 – user2189622 2013-04-22 09:56:44

回答

0

讓我們考慮,

的RadioButtonGroup ID:ControlTVRadioButtonGroup

單選按鈕這一組是:TV_on_rb和TV_off_rb

代碼:

ControlTVRadioButtonGroup= (RadioGroup) view.findViewById(R.id.ControlTVRadioButtonGroup); 

ControlTVRadioButtonGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      if(checkedId == R.id.TV_on_rb) 
      { 
        //code you have written in onclick of button (i.e for tv_on) 
      } 
      else if(checkedId == R.id.TV_off_rb) 
      { 
       //code you have written in onclick of button1 (i.e for tv_off) 
      } 
     } 
    }); 
0

我想你想看看使用CheckBox,而不是一個單選按鈕組。這是一個簡單的雙狀態按鈕(選中或取消選中)。從文檔:

final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id); 
if (checkBox.isChecked()) { 
    checkBox.setChecked(false); 
} 
+0

這不適用,因爲OP使用的是Android,它沒有實現Swing組件 – 2013-04-22 12:44:26

+0

@MattTaylor正確。對不起,我錯過了那個細節。 – 2013-04-22 12:45:14