|
|
|
android รบกวนช่วยดูโค้ดของแอพแอนดรอยด์ทีครับ[มือใหม่หัดทำ] |
|
|
|
|
|
|
|
Code (Android-Java)
using System;package com.example.fan2;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import java.io.*;
import java.net.*;
public class Fan extends Activity {
EditText editText1 = null;
Button open = null;
Button close = null;
public TextView thetext = null;
public static String IPSER = null;
public final static int PORT = 7000;
public Socket androidsocket;
public static int i = 0;
public static String coma = "";
public DataOutputStream controlout = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fan);
editText1 = (EditText) findViewById(R.id.editText1);
open = (Button) findViewById(R.id.button1);
close = (Button) findViewById(R.id.button2);
open = (Button) findViewById(R.id.button1);
open.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
i = 1;
coma = "y";
coma = coma.toLowerCase();
IPSER = editText1.getText().toString();
if(i==1);
try { androidsocket = new Socket(IPSER,PORT);
controlout = new
DataOutputStream(androidsocket.getOutputStream());
controlout.writeBytes(coma);
Toast.makeText(Fan.this,"opened",Toast.LENGTH_SHORT).show();
} catch (IOException e) {Toast.makeText(Fan.this,"not opened",Toast.LENGTH_SHORT).show();
}
}});
close = (Button) findViewById(R.id.button2);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
i = 0;
coma = "w";
coma = coma.toLowerCase();
IPSER = editText1.getText().toString();
if(i==0)
try { androidsocket = new Socket(IPSER,PORT);
controlout = new
DataOutputStream(androidsocket.getOutputStream());
controlout.writeBytes(coma);
Toast.makeText(Fan.this,"closed",Toast.LENGTH_SHORT).show();
} catch (IOException e) {Toast.makeText(Fan.this,"not closed",Toast.LENGTH_SHORT).show();
}
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fan, menu);
return true;
}
}
ช่วยตรวจสอบโค้ดให้หน่อยครับ
การทำงานประมาณว่าแค่ใส่ ip เข้าไป พอกดปุ่มก็จะทำงาน
ถ้าไม่ใส่โปรแกรมจะไม่ทำงาน
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2012-10-23 15:44:02 |
By :
apichad2 |
View :
1291 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ก็สร้าง method ขึ้นมาสำหรับการรับข้อมูลใน text แล้วตรวจสอบจำนวนการกรอกข้อมูลนิดหน่อยก็ได้ครับ
แล้วแต่จะเลือกใช้วิธีการ จะเลือกแบบ Dialog หรือ Toast หรืออะไรก็ได้
Code (Java)
using System;package com.example.fan2;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.app.AlertDialog;
import java.io.*;
import java.net.*;
public class Fan extends Activity {
EditText editText1 = null;
Button open = null;
Button close = null;
public TextView thetext = null;
public static String IPSER = null;
public final static int PORT = 7000;
public Socket androidsocket;
public static int i = 0;
public static String coma = "";
public DataOutputStream controlout = null;
private AlertDialog.Builder msg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fan);
msg = new AlertDialog.Builder(this);
msg.setTitle("Please Check info.");
msg.setPositiveButton(" X ", null);
editText1 = (EditText) findViewById(R.id.editText1);
open = (Button) findViewById(R.id.button1);
close = (Button) findViewById(R.id.button2);
open = (Button) findViewById(R.id.button1);
open.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
if (checkValid()) {
i = 1;
coma = "y";
coma = coma.toLowerCase();
IPSER = editText1.getText().toString();
if(i==1);
try {
androidsocket = new Socket(IPSER,PORT);
controlout = new
DataOutputStream(androidsocket.getOutputStream());
controlout.writeBytes(coma);
Toast.makeText(Fan.this,"opened",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(Fan.this,"not opened",Toast.LENGTH_SHORT).show();
}
}
}});
close = (Button) findViewById(R.id.button2);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i = 0;
coma = "w";
coma = coma.toLowerCase();
IPSER = editText1.getText().toString();
if(i==0)
try { androidsocket = new Socket(IPSER,PORT);
controlout = new
DataOutputStream(androidsocket.getOutputStream());
controlout.writeBytes(coma);
Toast.makeText(Fan.this,"closed",Toast.LENGTH_SHORT).show();
} catch (IOException e) {Toast.makeText(Fan.this,"not closed",Toast.LENGTH_SHORT).show();
}
}});
}
public boolean checkValid() {
alertShow = new AlertDialog.Builder(this);
alertShow.setTitle("Sorry, Please check input.");
alertShow.setPositiveButton("Close", null);
if (editText1.getText().length() == 0) {
alertShow.setMessage("Please input your IP Number.");
alertShow.show();
editText1.requestFocus();
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fan, menu);
return true;
}
}
ก็ประมาณนี้อ่ะจ้า
*** สุดแล้วก็แล้วแต่ท่านจะทำเน้อ เนื่องด้วย java มะเคยเรียนมาก่อนจ้า แบบว่าไปทาง php อ่ะจ้า
|
ประวัติการแก้ไข 2012-10-24 02:59:39 2012-10-24 03:04:00
|
|
|
|
Date :
2012-10-24 02:58:23 |
By :
iloveyoukiss |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2012-10-24 04:26:11 |
By :
apichad2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|