用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

用qq登录

2013-07-13 作者: 免费源代码下载整理举报

[android]代码库

package com.android.test.login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import util.ImagedbUtil;
import util.QQ;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class Date extends Activity implements OnClickListener,
		OnFocusChangeListener {

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.login_edit_pwd:
			if (hasFocus) {// 获得焦点,则获得密码
				String account = mAccountsEditText.getText().toString();
				if (account.equals("")) {
					break;//
				}
				if (list.containsKey(account)) {
					mPassEditText.setText(list.get(account));
				}
			}
			break;
		case R.id.login_edit_account:
			if (hasFocus) {
				mAccountsEditText.setText("");
				mPassEditText.setText("");
			}
			break;
		}
	}

	//
	ListView listView;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.popupwindow:
			if (pop == null) {
				if (adapter == null) {
					adapter = new myAdapter();
					listView = new ListView(Date.this);
					pop = new PopupWindow(listView,
							mAccountsEditText.getWidth(),
							LayoutParams.WRAP_CONTENT);
					listView.setAdapter(adapter);
					pop.showAsDropDown(mAccountsEditText);
				} else {
					account = list.keySet().toArray();
					adapter.notifyDataSetChanged();
					pop = new PopupWindow(listView,
							mAccountsEditText.getWidth(),
							LayoutParams.WRAP_CONTENT);
					pop.showAsDropDown(mAccountsEditText);
				}
			} else {
				pop.dismiss();
				pop = null;
			}
			break;
		case R.id.login_btn_login:
			if (mAccountsEditText.getText().toString().equals("")) {
				break;
			}
			String account = mAccountsEditText.getText().toString();
			String pass = mPassEditText.getText().toString();
			ImagedbUtil db = new ImagedbUtil(Date.this);
			db.open();
			Cursor cursor = db.getCursorArgs(new String[] { db.getKEY() },
					new String[] { account });
			int keyindex = cursor.getColumnIndexOrThrow(db.getKEY());
			if (mRemPassCheck.isChecked()) {
				// 保存密码
				if (cursor.getCount() > 0) {
					int id = cursor.getInt(keyindex);
					safeReleaseCursor(cursor);
					db.update(id, pass);
					safeReleaseDatabase(db);
				} else {
					safeReleaseCursor(cursor);
					db.create(account, pass);
					safeReleaseDatabase(db);
				}
				list.put(account, pass);// 重新替换或者添加记录
			}

			else {
				// 不保存密码
				if (cursor.getCount() > 0) {
					int id = cursor.getInt(keyindex);
					safeReleaseCursor(cursor);
					db.update(id, "");
					safeReleaseDatabase(db);
				} else {
					safeReleaseCursor(cursor);
					db.create(account, "");
					safeReleaseDatabase(db);
				}

				list.put(account, "");// 重新替换或者添加记录
			}
			mAccountsEditText.setText("");
			mPassEditText.setText("");
			break;
		}
	}

	LinearLayout popupLinear;
	ImageButton mPopupImageButton;
	public PopupWindow pop;
	public EditText mAccountsEditText;
	EditText mPassEditText;
	CheckBox mRemPassCheck;
	Button mLoginButton;
	public myAdapter adapter;
	public HashMap<String, String> list;
	Object[] account;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpage);
		prepare();
		mPopupImageButton = (ImageButton) findViewById(R.id.popupwindow);
		mRemPassCheck = (CheckBox) findViewById(R.id.login_cb_savepwd);
		mLoginButton = (Button) findViewById(R.id.login_btn_login);
		mAccountsEditText = (EditText) findViewById(R.id.login_edit_account);
		mPassEditText = (EditText) findViewById(R.id.login_edit_pwd);
		mPassEditText.setOnFocusChangeListener(this);
		mAccountsEditText.setOnFocusChangeListener(this);
		mPopupImageButton.setOnClickListener(this);
		mLoginButton.setOnClickListener(this);
	}

	private void prepare() {
		list = new HashMap<String, String>();
		ImagedbUtil db = new ImagedbUtil(this);
		db.open();
		Cursor cursor = db.getCursor(db.getKEY(), db.getACCOUNTS(),
				db.getPASSWORD());
		int accountsindex = cursor.getColumnIndexOrThrow(db.getACCOUNTS());
		int passindex = cursor.getColumnIndexOrThrow(db.getPASSWORD());
		String accounts;
		String pass;
		if (cursor.getCount() > 0) {
			do {
				accounts = cursor.getString(accountsindex);
				pass = cursor.getString(passindex);
				list.put(accounts, pass);
			} while (cursor.moveToNext());
		}
		safeReleaseCursor(cursor);
		safeReleaseDatabase(db);
	}

	private void safeReleaseCursor(Cursor cursor) {
		cursor.close();
		cursor = null;
	}

	private void safeReleaseDatabase(ImagedbUtil db) {
		db.close();
		db = null;
	}

	class myAdapter extends BaseAdapter {
		LayoutInflater mInflater;

		public myAdapter() {
			mInflater = LayoutInflater.from(Date.this);
			account = list.keySet().toArray();
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return account.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			Holder holder = null;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.popup, null);
				holder = new Holder();
				holder.view = (TextView) convertView.findViewById(R.id.mQQ);
				holder.button = (ImageButton) convertView
						.findViewById(R.id.mQQDelete);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			if (holder != null) {
				convertView.setId(position);
				holder.setId(position);
				holder.view.setText(account[position].toString());
				holder.view.setOnTouchListener(new OnTouchListener() {

					@Override
					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						pop.dismiss();
						mAccountsEditText.setText(account[position].toString());
						mPassEditText.setText(list.get(account[position]));
						return true;
					}
				});

				holder.button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String accounts = account[position].toString();
						list.remove(accounts);
						ImagedbUtil db = new ImagedbUtil(Date.this);
						db.open();
						Cursor cursor = db.getCursorArgs(
								new String[] { db.getKEY() },
								new String[] { accounts });
						int keyindex = cursor.getColumnIndexOrThrow(db.getKEY());
						int id = cursor.getInt(keyindex);
						cursor.close();
						db.delete(id);
						account = list.keySet().toArray();
						adapter.notifyDataSetChanged();
					}
				});
			}
			return convertView;
		}

		class Holder {
			TextView view;
			ImageButton button;

			void setId(int position) {
				view.setId(position);
				button.setId(position);
			}
		}

	}

}

[源代码打包下载]




网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...