两个不同的字符串通过使用wifi的单个切换按钮从android传递到pc

时间:2013-05-21 09:39:10

标签: java android wifi togglebutton

在我的android项目中,我已成功通过2个字符串[(电视ON(通过按钮1)和电视关闭(通过按钮2)]从我的Android手机到我的电脑通过2个简单的按钮使用WiFi。但在这里我需要使用单个切换按钮[(TV ON(单击切换按钮)和TV OFF(再次单击切换按钮)]而不是2个简单按钮(如上所述)传递2个字符串。 对不起,我的英语不好。 谢谢你。 java代码 -

        package com.example.wifitoggle;

        import java.io.IOException;
        import java.io.PrintWriter;
        import java.net.Socket;
        import java.net.UnknownHostException;
        import android.widget.ToggleButton;
        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 ToggleButton toggleButton1;
private String messsage;
int port = 0; //

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

 //   etIP = (EditText) findViewById(R.id.editText1);
  //  etPort = (EditText) findViewById(R.id.editText2);

    //etMsg = (EditText) findViewById(R.id.editText3);
    button = (Button) findViewById(R.id.button1);
    button1 = (Button) findViewById(R.id.button2);
    toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
    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();


    }
});


   }

     }

2 个答案:

答案 0 :(得分:0)

试试这个..

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){

                  //do something if on

            }else{

                 //do something if off               

        }
    });

替代

 toggleButton1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            if((toggleButton1.isChecked()))
            {

                 //do something if on

            }
            else
            {
                 //do something if off

            }
        }
    });

答案 1 :(得分:0)

这是正确的代码

  mToggleButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (mToggleButton.isChecked()) {
                messsage = "TV ON";
                Log.d("On", "Button On" + messsage);
                new Thread(new Runnable() {

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

            } else {

                messsage = "TV OFF";
                Log.d("off", "Button off " + messsage);// etMsg.getText().toString();
                // etMsg.setText("");
                // port = Integer.parseInt(etPort.getText().toString());

                new Thread(new Runnable() {

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

            }

        }
    });

    new Thread(new Runnable() {

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