-2

I am successfully passing string from my android to computer(through WiFi) using two simple button.but my question is i want to use single radio button (as toggle) instead of two simple button .here is my code

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


    }
});
       }

     }
4

2 回答 2

0

我认为您想研究使用 aCheckBox而不是单选按钮组。这是一个简单的两种状态按钮(选中或未选中)。从文档中:

final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
    checkBox.setChecked(false);
}
于 2013-04-22T12:41:00.990 回答
0

让我们考虑一下,

单选按钮组 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)
            }
        }
    });
于 2013-04-22T10:17:56.263 回答