用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

android登陆时记住密码后下次登陆密码自动键入!涉及下拉输入框, 文件存取,

2013-07-05 作者: 武汉小强举报

[android]代码库

package com.Ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.Dao.BaseDao;
import com.Dao.R;
import com.Entity.User;

public class UserLogInfo extends Activity {
	private Button btLogin, btAdd;
	private EditText password;
	private CheckBox check;
	private SharedPreferences sp;
	private String userNameValue, passwordValue;
	private AutoCompleteTextView userName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userlogin);
		/**
		 * 不解释
		 */
		btLogin = (Button) findViewById(R.id.btLogin);
		btAdd = (Button) findViewById(R.id.btAdd);
		userName = (AutoCompleteTextView) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		check = (CheckBox) findViewById(R.id.check);
		sp = this.getSharedPreferences("passwordFile", MODE_WORLD_READABLE);
		check.setChecked(true);
		btLogin.setOnClickListener(new button());
		btAdd.setOnClickListener(new button());
		/**
		 * 下拉输入框变更事件
		 */
		userName.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				/**
				 * 将账号跟密码存入passwordFile文件
				 */
				String[] allUserName = new String[sp.getAll().size()];
				allUserName = sp.getAll().keySet().toArray(new String[0]);
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(
						UserLogInfo.this, R.layout.test, allUserName);
				userName.setAdapter(adapter);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
			}

			/**
			 * 帐号变更后自动输入相对应的密码
			 */
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				password.setText(sp
						.getString(userName.getText().toString(), ""));
			}
		});
	}

	class button implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			userNameValue = userName.getText().toString();
			passwordValue = password.getText().toString();
			switch (v.getId()) {
			/**
			 * 登陆按钮事件
			 * 
			 */
			case R.id.btLogin:
				Toast.makeText(UserLogInfo.this, "Loading......", 0).show();
				BaseDao basedao = new BaseDao(UserLogInfo.this, "test.db");
				User user = new User();
				user.setUserName(userName.getText().toString());
				user.setPassWord(password.getText().toString());
				Cursor cursor = basedao.Login(user);
				/**
				 * 验证密码
				 */
				if (cursor.moveToNext()) {
					/**
					 * 判断记住密码复选框是否开启
					 */
					if (check.isChecked()) {
						sp.edit().putString(userNameValue, passwordValue)
								.commit();
					}
					Intent intent = new Intent(UserLogInfo.this, GetTrain.class);
					startActivity(intent);
					Toast.makeText(UserLogInfo.this, "登陆成功!", 0).show();
				} else {
					Toast.makeText(UserLogInfo.this, "密码错误!", 0).show();
				}
				break;
			/**
			 * 注册按钮点击事件
			 */
			case R.id.btAdd:
				final RelativeLayout layout = (RelativeLayout) getLayoutInflater()
						.inflate(R.layout.adduser, null);
				new AlertDialog.Builder(UserLogInfo.this)
						.setTitle("用户注册")
						.setView(layout)
						.setPositiveButton("提交",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										EditText username = (EditText) layout
												.findViewById(R.id.username);
										EditText password = (EditText) layout
												.findViewById(R.id.password);
										EditText passagain = (EditText) layout
												.findViewById(R.id.passagain);
										if (password
												.getText()
												.toString()
												.equals(passagain.getText()
														.toString())) {
											BaseDao basedao = new BaseDao(
													UserLogInfo.this, "test.db");
											User user = new User();
											user.setUserName(username.getText()
													.toString());
											user.setPassWord(password.getText()
													.toString());
											basedao.addUser(user);
											Toast.makeText(UserLogInfo.this,
													"注册成功!", 0).show();
										} else {
											Toast.makeText(UserLogInfo.this,
													"两次密码输入不一致!", 0).show();
										}
									}
								}).setNegativeButton("取消", null).show();

				break;
			default:
				break;
			}
		}
	}
}

[代码运行效果截图]


android登陆时记住密码后下次登陆密码自动键入!涉及下拉输入框, 文件存取,


网友评论    (发表评论)


发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...