0

私のAndroidプロジェクトでは、WiFiを使用して2つの簡単なボタンでAndroidフォンからPCに2つの文字列[(TV ON(ボタン1による)とTV OFF(ボタン2による)]を正常に渡しましたが、ここでは2を渡す必要があります単一のトグル ボタン [(TV ON(トグル ボタンをクリック) と TV OFF(再びトグル ボタンをクリック)] を使用する文字列) 2 つの単純なボタン (上記で説明) の代わりに。 -

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


    }
});


   }

     }
4

2 に答える 2

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

            }
        }
    });
于 2013-05-21T09:47:56.950 に答える
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();
于 2013-05-23T06:27:22.357 に答える