android 仿QQ登录界面 抽屉实现
package com.b509.login;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;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.view.Window;import android.widget.CheckBox;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.SlidingDrawer;import android.widget.Toast;import com.b509.R;import com.b509.activity.doctor.DoctorChooseFunctionActivity;import com.b509.activity.patient.ChooseFunctionActivity;import com.b509.activity.patient.mydoctors.service.LoginService;import com.b509.tools.GlobalApplication;import com.b509.tools.MD5;import com.b509.tools.Tools;public class LoginActivity extends Activity { private static String TAG = "Login"; EditText et_username, et_password; CheckBox cb_remember; RadioGroup radiogroup; RadioButton rb_doc, rb_pat; ImageButton ib_login; ImageButton ib_reg; ImageButton help; ImageButton about; String role; int role_position; String roles[]; public static LoginActivity instance; Intent intent; Context con = LoginActivity.this; private SlidingDrawer sd; private ImageView iv; GlobalApplication ga; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login); setLoginInfo(); instance = this; sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { public void onDrawerOpened() { iv.setImageResource(R.drawable.drawer_down); } }); sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { public void onDrawerClosed() { iv.setImageResource(R.drawable.drawer_up); } }); } private void setLoginInfo() { roles = new String[] { "医生", "患者" }; intent = new Intent(); et_username = (EditText) findViewById(R.id.login_et_username); et_password = (EditText) findViewById(R.id.login_et_password); iv = (ImageView) findViewById(R.id.handle); sd = (SlidingDrawer) findViewById(R.id.slidingDrawer); radiogroup = (RadioGroup) findViewById(R.id.login_role); rb_doc = (RadioButton) findViewById(R.id.login_role_doc); rb_pat = (RadioButton) findViewById(R.id.login_role_pat); cb_remember = (CheckBox) findViewById(R.id.login_cb_remember); ib_login = (ImageButton) findViewById(R.id.login_b); ib_reg = (ImageButton) findViewById(R.id.login_b_reg); help = (ImageButton) findViewById(R.id.login_ib_help); about = (ImageButton) findViewById(R.id.login_ib_about); listener(); getLoginParams(); } // 登陆事件 private void listener() { ib_login.setOnClickListener(new OnClickListener() { public void onClick(View v) { String msg = "正在连接服务器..."; ProgressDialog pd = Tools.pd(instance, msg); checkLoginPSWD(pd); } }); radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) {// 设置角色 if (checkedId == rb_doc.getId()) { role_position = 0; } else { role_position = 1; } System.out.println(role_position); } }); ib_reg.setOnClickListener(new OnClickListener() { public void onClick(View v) {// 用户注册 intent.setClass(con, RegisterActivity.class); startActivity(intent); } }); help.setOnClickListener(new OnClickListener() { public void onClick(View v) { intent.setClass(con, HelpActivity.class); startActivity(intent); } }); about.setOnClickListener(new OnClickListener() { public void onClick(View v) { intent.setClass(con, AboutActivity.class); startActivity(intent); } }); } private void checkLoginPSWD(final ProgressDialog pd) { new Thread() { public void run() {// 用户登陆 String user = et_username.getText().toString().trim(); String pw = et_password.getText().toString().trim(); pw = MD5.getMD5Str(pw); String role = roles[role_position]; try { String message = LoginService.login(user, pw, role); if (message.equals("医生")) { ga = (GlobalApplication) getApplication(); ga.setUsername(user); ga.setRole("医生"); Message mess = new Message(); mess.what = 1; handler.sendMessage(mess); } else if (message.equals("患者")) { ga = (GlobalApplication) getApplication(); ga.setUsername(user); ga.setRole("患者"); Message mess = new Message(); mess.what = 2; handler.sendMessage(mess); } else if (message.equals("fail")) { Message mess = new Message(); mess.what = 3; handler.sendMessage(mess); } pd.dismiss(); } catch (Exception e) { e.printStackTrace(); } } }.start(); } private void saveLoginParams() { SharedPreferences sp = getSharedPreferences("loginparam", Activity.MODE_PRIVATE); boolean isChecked = cb_remember.isChecked(); if (isChecked) { sp.edit() .putString("userName", et_username.getText().toString().trim()) .putString("userPassword", et_password.getText().toString().trim()) .putInt("position", role_position) .putBoolean("isChecked", true).commit(); } else { sp.edit().putString("userName", "").putString("userPassword", "") .putInt("position", 0).putBoolean("isChecked", false) .commit(); } } Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: saveLoginParams(); Tools.displayToast(con, "登录成功"); intent.setClass(con, DoctorChooseFunctionActivity.class); startActivity(intent); break; case 2: saveLoginParams(); Tools.displayToast(con, "登录成功"); intent.setClass(con, ChooseFunctionActivity.class); startActivity(intent); break; case 3: Tools.displayToast(con, "登录失败请检查你的用户名、密码、角色是否正确!"); break; } } }; // 获取登陆数据 private void getLoginParams() { SharedPreferences sp = getSharedPreferences("loginparam", Activity.MODE_PRIVATE); boolean isChecked = sp.getBoolean("isChecked", false); if (isChecked) { et_username.setText(sp.getString("userName", "")); et_password.setText(sp.getString("userPassword", "")); System.out.println(sp.getInt("position", 0)+"得到的role"); if (sp.getInt("position", 0) == 0) { rb_doc.setChecked(true); System.out.println("执行医生"); } else { rb_pat.setChecked(true); System.out.println("执行病人"); } cb_remember.setChecked(true); } } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { new AlertDialog.Builder(con) .setTitle("提示") .setMessage("你确定退出程序吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { android.os.Process .killProcess(android.os.Process .myPid());// 结束进程,退出程序 } }).setNegativeButton("取消", null).show(); } return false; }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/iq" android:orientation="vertical"> <EditText android:id="@+id/login_et_username" android:layout_width="240dip" android:layout_height="50dip" android:layout_gravity="center_horizontal" android:hint="账号" android:textSize="16sp" android:layout_marginTop="150dip" android:layout_centerHorizontal="true" /> <EditText android:id="@+id/login_et_password" android:layout_width="240dip" android:layout_height="50dip" android:layout_gravity="center_horizontal" android:hint="密码" android:password="true" android:textSize="16sp" android:layout_below="@id/login_et_username" android:layout_centerHorizontal="true"/> <ImageButton android:id="@+id/login_b" android:layout_width="235dip" android:layout_height="35dip" android:src="@drawable/lbutton" android:layout_below="@id/login_et_password" android:layout_centerHorizontal="true" /> <CheckBox android:id="@+id/login_cb_remember" android:layout_width="100dip" android:layout_height="30dip" android:text="记住密码" android:textColor="#ff000000" android:textSize="10sp" android:checked="false" android:layout_below="@id/login_b" android:layout_marginLeft="40dip" android:layout_marginTop="5dip" /> <ImageButton android:id="@+id/login_b_reg" android:layout_width="80dip" android:layout_height="30dip" android:layout_below="@id/login_b" android:layout_marginLeft="195dip" android:background="@drawable/reg" android:layout_marginTop="5dip" /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <SlidingDrawer android:id="@+id/slidingDrawer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:handle="@+id/handle" android:content="@+id/content" android:topOffset="320dip" > <ImageView android:id="@id/handle" android:layout_width="480dip" android:layout_height="30dip" android:paddingRight="10dip" android:src="@drawable/drawer_up" /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@id/content"> <RadioGroup android:id="@+id/login_role" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="10dip" > <RadioButton android:textColor="#000000" android:id="@+id/login_role_doc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="35dip" android:text="医生" android:textSize="18sp" /> <RadioButton android:textColor="#000000" android:id="@+id/login_role_pat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="患者" android:textSize="18sp" android:layout_marginLeft="70dip" /> </RadioGroup> <ImageButton android:id="@+id/login_ib_help" android:layout_width="100dip" android:layout_height="50dip" android:layout_below="@id/login_role" android:layout_marginLeft="180dip" android:background="@drawable/help" /> <ImageButton android:id="@+id/login_ib_about" android:layout_width="100dip" android:layout_height="50dip" android:layout_below="@id/login_role" android:background="@drawable/about" android:layout_marginLeft="40dip" /> </RelativeLayout> </SlidingDrawer> </LinearLayout></RelativeLayout>