iT邦幫忙

0

<<急>>如何合併兩段程式碼有關Android手機程式傳輸電腦

懇請各位高手大大們幫個忙

private static final String TAG = "MainActivity";
private Button mTotal;
private Button mChinese;
private Button mJapan;
private Button mSmail;
private Button mLove;
private Button mTotal1;
private Button mChinese1;
private Button mJapan1;
private Button mSmail1;
private Button mLove1;
private Button mSend;
private TextView mTotal2;
private TextView mChinese2;
private TextView mJapan2;
private TextView mSmail2;
private TextView mLove2;
int counter = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTotal1 = (Button) findViewById(R.id.button6);
    mChinese1 = (Button) findViewById(R.id.button7);
    mJapan1 = (Button)findViewById(R.id.button8);
    mSmail1 = (Button)findViewById(R.id.button9);
    mLove1 = (Button)findViewById(R.id.button10);
    mTotal2 = (TextView)  findViewById(R.id.textView);
    mChinese2 = (TextView)  findViewById(R.id.textView2);
    mJapan2 = (TextView)  findViewById(R.id.textView3);
    mSmail2 = (TextView)  findViewById(R.id.textView4);
    mLove2 = (TextView)  findViewById(R.id.textView5);


    mTotal1.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            counter++;
            mTotal2.setText(String.valueOf(counter));
        }
    });
    mChinese1.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            counter2++;
            counter2 = counter2 % 7;
            mChinese2.setText(String.valueOf(counter2));
        }
    });
    mJapan1.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            counter3++;
            counter3 = counter3 % 7;
            mJapan2.setText(String.valueOf(counter3));
        }
    });
    mSmail1.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            counter4++;
            counter4 = counter4 % 7;
            mSmail2.setText(String.valueOf(counter4));
        }
    });
    mLove1.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            counter5++;
            counter5 = counter5 % 7;
            mLove2.setText(String.valueOf(counter5));
        }
    });     
    }
    

上方這段程式碼與下方這段程式碼兩段是否可以合併如果不是請問題錯在哪?謝謝各位高手大大們

public class Client{

    private Runnable Connection=new Runnable(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{
                InetAddress serverIp = InetAddress.getByName("192.168.0.1");
                int serverPort = 5050;
                clientSocket = new Socket(serverIp, serverPort);
                bw = new BufferedWriter( new OutputStreamWriter(clientSocket.getOutputStream()));
                br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                while (clientSocket.isConnected()) {
                    tmp = br.readLine();    
                    if(tmp!=null){

                        tmp=tmp.substring(tmp.indexOf("{"), tmp.lastIndexOf("}") + 1);
                        json_read=new JSONObject(tmp);
                        
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
                Log.e("text","Socket連線="+e.toString());
                finish();   
            }
        }
    };
    @Override
    protected void onDestroy() {   
        super.onDestroy();
        try {
            json_write=new JSONObject();
            json_write.put("action","離線");
            Log.i("text","onDestroy()="+json_write+"\n");
            bw.write(json_write+"\n");
            bw.flush();
            bw.close();
            br.close();
            clientSocket.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("text","onDestroy()="+e.toString());
        }
    }
}
}







}

謝謝各位高手大大們


2 則留言

0
fysh711426
iT邦新手 3 級 ‧ 2017-11-17 00:01:29

感覺上面那段程式碼似曾相似,
大大您沒有說明,需要怎麼樣的合併法?

0
andy6804tw
iT邦新手 5 級 ‧ 2017-11-17 10:24:00

setOnClickListener 可以寫在一起

我要留言

立即登入留言