android学习——SP方法实现用户登录

上两篇写完用户第一次启动欢迎界面的实现,那么欢迎界面结束后进入的应该就是用户登录界面,因为是菜鸟,就用最简单的方法去做了一个,当然还不支持注册等功能。


首先创建一个login_activity.xml并进行布局,以下是我的布局代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login" 
    android:orientation="vertical"
    tools:context="com.linlif.cv.activity.MainActivity" >

    <EditText
        android:id="@+id/uname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="310dp"
        android:background="#ffffff"
        android:hint="@string/username"
        android:padding="5dp"
        android:textColor="#000000"
        android:textSize="14sp" />

    <EditText
        android:id="@+id/upwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:background="#ffffff"
        android:hint="@string/userpwd"
        android:padding="5dp"
        android:textColor="#000000"
        android:textSize="14sp" />

    <CheckBox
        android:id="@+id/chb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:checked="true"
        android:text="@string/rememberpwd"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/shape"
        android:onClick="login"
        android:text="@string/login"
        android:textColor="#ffffff" />

</LinearLayout>


2,创建一个LoginActivity,代码如下

public class LoginActivity extends Activity {

	private EditText uname;
	private EditText upwd;
	private CheckBox chb;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);

		//找到需要用到的控件
		uname = (EditText) findViewById(R.id.uname);
		upwd = (EditText) findViewById(R.id.upwd);
		chb = (CheckBox) findViewById(R.id.chb);
		
		//实例化SharedPreferences对象
		sp = getSharedPreferences("info", 0);
		
		//获取info文件中的name和pwd
		String name = sp.getString("name", "");
		String pwd = sp.getString("pwd", "");
		
		//将info中的name和pwd显示在edittext中
		uname.setText(name);
		upwd.setText(pwd);
	}

	//登陆按钮的onclick属性为login,直接写login方法
	public void login(View v) {
		
		//获取edittext中的内容
		String name = uname.getText().toString().trim();
		String pwd = upwd.getText().toString().trim();
		
		//判断其中一个是否为空
		if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
			
			//其中一个为空则弹出一个toast,这里用到了自定义的ToastUtil,下一篇讲一下自定义Toast
			ToastUtil.makeText(LoginActivity.this, R.drawable.toast_login, "不知道填用户名和密码?你傻啊!", Toast.LENGTH_SHORT).show();

		} else {
			
			//判断checkbox是否被选中
			if (chb.isChecked()) {
				
				//选中则传入数据
				Editor edit = sp.edit();
				edit.putString("name", name);
				edit.putString("pwd", pwd);
				edit.commit();
				
				//进入下一个Activity
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
			} else {
				
				//没选中则弹出一个对话框
				ToastUtil.makeText(LoginActivity.this, R.drawable.toast_pwd, "求求你勾选上吧", Toast.LENGTH_SHORT).show();
			}
		}

	}
}


3,创建一个main_activity,布局以下登陆后的界面,结束,就是这么简单

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您解答关于Android开发的问题。以下是登录注册页面的示例代码: 1. activity_login.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:textSize="28sp" android:textColor="@android:color/black" android:layout_gravity="center_horizontal"/> <EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:hint="Email"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Password" android:inputType="textPassword"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Login"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Don't have an account?" android:textColor="@android:color/black"/> <TextView android:id="@+id/tv_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Register" android:textColor="@android:color/holo_blue_light"/> </LinearLayout> ``` 2. activity_register.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Register" android:textSize="28sp" android:textColor="@android:color/black" android:layout_gravity="center_horizontal"/> <EditText android:id="@+id/et_fullname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:hint="Full Name"/> <EditText android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Email"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:hint="Password" android:inputType="textPassword"/> <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Register"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Already have an account?" android:textColor="@android:color/black"/> <TextView android:id="@+id/tv_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:textColor="@android:color/holo_blue_light"/> </LinearLayout> ``` 3. LoginActivity.java ``` public class LoginActivity extends AppCompatActivity { private EditText etEmail, etPassword; private Button btnLogin; private TextView tvRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); etEmail = findViewById(R.id.et_email); etPassword = findViewById(R.id.et_password); btnLogin = findViewById(R.id.btn_login); tvRegister = findViewById(R.id.tv_register); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Perform login logic } }); tvRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); } }); } } ``` 4. RegisterActivity.java ``` public class RegisterActivity extends AppCompatActivity { private EditText etFullname, etEmail, etPassword; private Button btnRegister; private TextView tvLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); etFullname = findViewById(R.id.et_fullname); etEmail = findViewById(R.id.et_email); etPassword = findViewById(R.id.et_password); btnRegister = findViewById(R.id.btn_register); tvLogin = findViewById(R.id.tv_login); btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Perform registration logic } }); tvLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); } }); } } ``` 这个示例代码可以作为一个基础框架供您的开发使用。当然,您需要根据您的具体要求对代码进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值