android小項目

LoginActivity:java

package cn.thewee.gourmetmeal.staff;android

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;app

public class LoginActivity extends BaseActivity {ide

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

 /**
  * 控件變量初始化
  */
 @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();
   }
  });
 }this

 
 //登錄線程
 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){.net

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

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

}

 

 

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>

相關文章
相關標籤/搜索