android小项目

原创
2015/05/08 15:17
阅读数 18

LoginActivity:

package cn.thewee.gourmetmeal.staff;

import java.util.HashMap;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.thewee.gourmetmeal.staff.logic.ImgDownloadLogic;
import cn.thewee.gourmetmeal.staff.logic.LoginLogic;
import cn.thewee.gourmetmeal.staff.util.Tools;

public class LoginActivity extends BaseActivity {

 private static final String TAG = "LoginActivity";
 EditText edt_username;
 EditText edt_password;
 Button btn_login;
 
 //登陆结果处理,用来接收登陆线程消息,进行UI操作
 Handler login_handler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   //关闭进度条Dialog
   if(p_dialog.isShowing()){
    p_dialog.dismiss();
   }
   //如果登陆成功则进入MainActivity,否则toast提示错误
   switch(msg.what){
   case WHAT_SUBMIT_COMPLETE:
    if(msg.getData().getInt("code") == 200){
     Intent i_loginSuccess = null;
     if(edt_password.getText().toString().equals("888888")){
      i_loginSuccess = new Intent(LoginActivity.this, AlterPasswordActivity.class);
     }else{
      i_loginSuccess = new Intent(LoginActivity.this, MainActivity.class);
     }
     i_loginSuccess.putExtra("username", edt_username.getText().toString());
     startActivity(i_loginSuccess);
     LoginActivity.this.finish();
    }else{
     Toast.makeText(LoginActivity.this, msg.getData().getString("msg"), Toast.LENGTH_SHORT).show();
    }
    break;
   case WHAT_SUBMIT_FAILED:
    Toast.makeText(LoginActivity.this, getString(R.string.net_timeout), Toast.LENGTH_SHORT).show();
    break;
   }
   
  }
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //配置Listener
  setListener();
 }

 /**
  * 控件变量初始化
  */
 @Override
 protected void initViews() {
  this.setContentView(R.layout.login);
  
  edt_username = (EditText) this.findViewById(R.id.edt_username);
  edt_password = (EditText) this.findViewById(R.id.edt_password);
  btn_login = (Button) this.findViewById(R.id.btn_login);
  //登陆进度条
  p_dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  p_dialog.setMessage(getString(R.string.is_logining));
 }
 
 /**
  * 配置Listener
  */
 private void setListener() {
  //登陆按钮监听器
  btn_login.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View arg0) {
    //检测文本框是否为空
    if(edt_username.getText().length() == 0 || edt_password.getText().length() == 0){
     Toast.makeText(LoginActivity.this,
       getString(R.string.text_is_null), Toast.LENGTH_SHORT).show();
     return;
    }
    //检测网络是否可用,若不可用Toast提示
    if(!Tools.netIsAvailable(getApplicationContext())){
     Toast.makeText(LoginActivity.this,
         getString(R.string.net_disable), Toast.LENGTH_SHORT).show();
     return;
    }
    
    //显示进度条
    p_dialog.show();
    //开启登陆线程
    LoginThread loginThread = new LoginThread();
    loginThread.setDaemon(true);
    loginThread.start();
   }
  });
 }

 
 //登陆线程
 class LoginThread extends Thread{
  @Override
  public void run() {
   Message msg = login_handler.obtainMessage();
   Bundle data = new Bundle();
   //获取登陆结果
   LoginLogic loginLogic = new LoginLogic();
   HashMap<String, Object> result =  loginLogic.login(edt_username.getText().toString(),
               edt_password.getText().toString());
//   HashMap<String, Object> result = new HashMap<String, Object>();
//   result.put("rt", "200");
//   result.put("rtmsg", "登陆成功");
   if(result!=null){
    msg.what = WHAT_SUBMIT_COMPLETE;
    data.putInt("code", Integer.valueOf((String)result.get("rt")));
    data.putString("msg", (String)result.get("rtmsg"));
    msg.setData(data);
    //发送消息
    login_handler.sendMessage(msg);
   }else{
    login_handler.sendEmptyMessage(WHAT_SUBMIT_FAILED);
   }
  }
 }
 
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
     new AlertDialog.Builder(LoginActivity.this)
     .setIcon(R.drawable.ic_launcher)
     .setTitle("提示")
     .setMessage("确认退出?")
        .setPositiveButton("确定",
                       new DialogInterface.OnClickListener(){
                               public void onClick(DialogInterface dialoginterface, int i){
                                System.exit(1);
                                }
                        })
        .setNeutralButton("返回",
                       new DialogInterface.OnClickListener(){
                               public void onClick(DialogInterface dialoginterface, int i){

                                }
                        })                        
        .show();
  return super.onKeyDown(keyCode, event);
 }

 @Override
 protected void getExtras() {
  // TODO Auto-generated method stub
  
 }

}

 

 

login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@drawable/menu_background" >


    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="50dp"
        android:paddingTop="30dp"
        android:background="@drawable/login_bg" >
  
        <TextView
            android:id="@+id/loginnametextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="65dp"
            android:layout_toLeftOf="@+id/edtxt_login_pwd"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="20sp" >
        </TextView>
        <EditText
            android:id="@+id/edt_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            android:layout_marginLeft="10dp"
            android:layout_alignTop="@+id/loginnametextview"
            android:layout_toRightOf="@+id/loginnametextview"
            android:background="@drawable/input_bg"
            android:hint="@string/username_hint"
            android:singleLine="true"
            android:text="login01" >
        </EditText>
       
        <TextView
            android:id="@+id/loginpasswordtextview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/edt_username"
            android:layout_marginLeft="65dp"
            android:layout_marginTop="15dp"
            android:text="@string/password"
            android:textColor="#000000"
            android:textSize="20sp" >
        </TextView>
         <EditText
            android:id="@+id/edt_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_marginLeft="10dp"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/loginpasswordtextview"
            android:layout_toRightOf="@+id/loginpasswordtextview"
            android:background="@drawable/input_bg"
            android:inputType="textPassword"
            android:hint="@string/password_hint"
            android:text="999999" >
        </EditText>
       
         <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/edt_password"
            android:layout_marginTop="15dp"
            android:layout_alignRight="@+id/edt_password"
            android:background="@drawable/btn_login" >
        </Button>
       

    </RelativeLayout>

</RelativeLayout>

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部