//登录的login.xml
//在Androidmanifest.xml添加允许网络通信的语句
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity"
android:background="#29aac0">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="128dp"
android:gravity="center"
android:text="民大招生系统"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="25dp" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dip">
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输用户名"
android:textColor="#000" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输登陆密码"
android:inputType="textPassword"
android:textColor="#000" >
</EditText>
</TableLayout>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dip" >
<Button
android:id="@+id/btlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="50dip"
android:background="#39b55f"
android:text="登陆" />
<Button
android:id="@+id/btreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="50dip"
android:background="#30ff0f"
android:text="重置" />
<Button
android:id="@+id/btregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="50dip"
android:background="#0000ff"
android:text="注册" />
</TableRow>
</TableLayout>
</TableLayout>
//Activity Java代码
public class LoginActivity extends Activity {
EditText user,pass;
Button login,reset,register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
findmyid();
//为每个button添加点击事件
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
login();
}
});
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
reset();
}
});
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
register();
}
});
}
//为每个控件匹配ID
private void findmyid() {
// TODO Auto-generated method stub
user=(EditText) findViewById(R.id.username);
pass=(EditText) findViewById(R.id.password);
login=(Button) findViewById(R.id.btlogin);
reset=(Button) findViewById(R.id.btreset);
register=(Button) findViewById(R.id.btregister);
}
/*
*每个button的事件定义
*/
//登陆事件
protected void login(){
try{
// 定义源地址
final String PostUrl = "http://10.0.2.2/zhaosheng/index.php/AndroidLogin/login";
// 创建一个Http请求
HttpPost httprequest = new HttpPost(PostUrl);
List postparams = new ArrayList();
postparams.add(new BasicNameValuePair("username",user.getText().toString()));
// 添加必须的参数
postparams.add(new BasicNameValuePair("password",pass.getText().toString()));
httprequest.setEntity(new UrlEncodedFormEntity(postparams, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httprequest);
// 发送请求并获取反馈
// 解析返回的内容
if (httpResponse.getStatusLine().getStatusCode() != 404)
{
String result = EntityUtils.toString(httpResponse.getEntity());
Toast.makeText(getApplicationContext(), result,Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
}
}
//重置事件
protected void reset(){
user.setText("");
pass.setText("");
}
//注册事件
protected void register(){
}
}
//php服务端(写个控制器)
<?php
//与AndroidApp交互的控制器
class AndroidLoginAction extends Action {
public function index(){
}
//用户登录方法
public function login(){
$user=M('user');
$username=I("username");
$password=I("password");
$condition['username']=$username;
$condition['password']=$password;
if($user->where($condition)->select()){
echo "SUCCESS";
}
else{
echo "ERROR";
}
}
//用户注册方法
public function register(){
}
}
?>
//在Androidmanifest.xml添加允许网络通信的语句
<uses-permission android:name="android.permission.INTERNET" />