android开发实例之minitwitter登录界面 代码,MiniTwitter记住密码等功能实现

7705f529dd17934e9ff2f7c784820e05.png

MainActivity.java

package com.liu.activity;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.EditText;

import android.widget.ImageButton;

import android.widget.Toast;

public class LoginActivity extends Activity {

private EditText userName, password;

private CheckBox rem_pw, auto_login;

private Button btn_login;

private ImageButton btnQuit;

private String userNameValue,passwordValue;

private SharedPreferences sp;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//去除标题

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.login);

//获得实例对象

sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);

userName = (EditText) findViewById(R.id.et_zh);

password = (EditText) findViewById(R.id.et_mima);

rem_pw = (CheckBox) findViewById(R.id.cb_mima);

auto_login = (CheckBox) findViewById(R.id.cb_auto);

btn_login = (Button) findViewById(R.id.btn_login);

btnQuit = (ImageButton)findViewById(R.id.img_btn);

//判断记住密码多选框的状态

if(sp.getBoolean("ISCHECK", false))

{

//设置默认是记录密码状态

rem_pw.setChecked(true);

userName.setText(sp.getString("USER_NAME", ""));

password.setText(sp.getString("PASSWORD", ""));

//判断自动登陆多选框状态

if(sp.getBoolean("AUTO_ISCHECK", false))

{

//设置默认是自动登录状态

auto_login.setChecked(true);

//跳转界面

Intent intent = new Intent(LoginActivity.this,LogoActivity.class);

LoginActivity.this.startActivity(intent);

}

}

// 登录监听事件  现在默认为用户名为:xuyinghui 密码:123

btn_login.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

userNameValue = userName.getText().toString();

passwordValue = password.getText().toString();

if(userNameValue.equals("xuyinghui")&&passwordValue.equals("123"))

{

Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();

//登录成功和记住密码框为选中状态才保存用户信息

if(rem_pw.isChecked())

{

//记住用户名、密码、

Editor editor = sp.edit();

editor.putString("USER_NAME", userNameValue);

editor.putString("PASSWORD",passwordValue);

editor.commit();

}

//跳转界面

Intent intent = new Intent(LoginActivity.this,LogoActivity.class);

LoginActivity.this.startActivity(intent);

//finish();

}else{

Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();

}

}

});

//监听记住密码多选框按钮事件

rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if (rem_pw.isChecked()) {

System.out.println("记住密码已选中");

sp.edit().putBoolean("ISCHECK", true).commit();

}else {

System.out.println("记住密码没有选中");

sp.edit().putBoolean("ISCHECK", false).commit();

}

}

});

//监听自动登录多选框事件

auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if (auto_login.isChecked()) {

System.out.println("自动登录已选中");

sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

} else {

System.out.println("自动登录没有选中");

sp.edit().putBoolean("AUTO_ISCHECK", false).commit();

}

}

});

btnQuit.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

}

}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@drawable/loginbg"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

"

login.xml:

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/tvRegist"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="21dp"

android:layout_marginTop="18dp"

android:text="@string/tvRegister"

android:autoLink="all"

android:textColorLink="#FF0066CC" />

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_marginBottom="24dp"

android:src="@drawable/panda" />

android:id="@+id/imageView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="28dp"

android:src="@drawable/icon" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/btnbg_roundcorner"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin" >

android:id="@+id/tvUsername"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="@string/tvName"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/etUsername"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/tvUsername"

android:layout_below="@+id/tvUsername"

android:background="@android:drawable/edit_text"

android:ems="10" >

android:id="@+id/tvPassword"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/etUsername"

android:layout_below="@+id/etUsername"

android:text="@string/tvPassword"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/etPassword"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/tvPassword"

android:layout_below="@+id/tvPassword"

android:layout_marginTop="16dp"

android:background="@android:drawable/edit_text"

android:ems="10"

android:inputType="textPassword" />

android:id="@+id/btnLogin"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignRight="@+id/etPassword"

android:layout_below="@+id/etPassword"

android:layout_marginTop="20dp"

android:background="#FF72CAE1"

android:text="@string/btnLogin" />

android:id="@+id/cbRememberPass"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/etPassword"

android:layout_alignTop="@+id/btnLogin"

android:text="记住密码" />

原文:http://www.cnblogs.com/aleale/p/4619965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android Studio登录界面代码,可以记住密码: 1. 创建一个新的Android Studio项目,然后在布局文件中添加以下代码: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <EditText android:id="@+id/usernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" /> <EditText android:id="@+id/passwordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="Password" /> <CheckBox android:id="@+id/rememberMeCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Remember me" /> <Button android:id="@+id/loginButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" /> </LinearLayout> ``` 2. 在Java文件中添加以下代码: ```java public class LoginActivity extends AppCompatActivity { private EditText usernameEditText, passwordEditText; private CheckBox rememberMeCheckBox; private Button loginButton; private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; private static final String SHARED_PREFS = "sharedPrefs"; private static final String USERNAME = "username"; private static final String PASSWORD = "password"; private static final String REMEMBER_ME = "rememberMe"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameEditText = findViewById(R.id.usernameEditText); passwordEditText = findViewById(R.id.passwordEditText); rememberMeCheckBox = findViewById(R.id.rememberMeCheckBox); loginButton = findViewById(R.id.loginButton); sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE); editor = sharedPreferences.edit(); // 如果已经保存了用户名和密码,则自动填写 if (sharedPreferences.getBoolean(REMEMBER_ME, false)) { usernameEditText.setText(sharedPreferences.getString(USERNAME, "")); passwordEditText.setText(sharedPreferences.getString(PASSWORD, "")); rememberMeCheckBox.setChecked(true); } loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = usernameEditText.getText().toString().trim(); String password = passwordEditText.getText().toString().trim(); // 检查用户名和密码是否为空 if (TextUtils.isEmpty(username)) { Toast.makeText(LoginActivity.this, "Please enter username", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(LoginActivity.this, "Please enter password", Toast.LENGTH_SHORT).show(); return; } // 如果勾选了“记住我”,则保存用户名和密码 if (rememberMeCheckBox.isChecked()) { editor.putString(USERNAME, username); editor.putString(PASSWORD, password); editor.putBoolean(REMEMBER_ME, true); editor.apply(); } else { editor.clear(); editor.apply(); } // 登录逻辑 // ... } }); } } ``` 这个简单的登录界面具备记住密码功能,如果用户勾选了“记住我”复选框,则会保存用户名和密码,并在下次打开应用时自动填写。如果用户未勾选,则会清除保存的用户名和密码。您可以根据自己的需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值