我计划为句子迷做一个完整的登录注册功能,但因为用到的东西比较多,所以我就做一点,总结一点。同时因为这是我们的合作项目嘛,我不能自己想写点什么就往上加,我就自己又新建了一个工程来完成这部分功能,等到全做好了,如果讨论了能用,就加到整个项目当中。我目前完成了点击登录,跳转,后续呢还要再加入数据库,把注册过的账号保存起来,在点击登录之后进行核实,还需要为新用户写注册功能,先请大家看看我已经完成的部分吧:
记住密码后第二次打开的样子:
点击登录后:
差不多就是这样的,给大家看代码
MainActivity
package com.example.lenovo.edittext_part;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private EditText editTextb;
private Drawable drawable;
private Drawable drawableb;
private TextWatcher textWatcher;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
private boolean mIsChecked=false;
private Button btn_sign_in; //登录按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_sign_in=findViewById(R.id.btn_sign_in);
setEdit();
setView();
signin();
}
//这个函数用于对点击登录按钮事件做出响应,完成登录功能
private void signin() {
btn_sign_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**核对账号密码,如果都没有问题的话就实现跳转,跳转到一个新的界面,本质是跳转到一个
* 新的Activity.采用intent对两个Activity进行通信连接*/
/**★★★注意这个判空条件*/
if(TextUtils.isEmpty(editText.getText())||TextUtils.isEmpty(editTextb.getText())){
Log.d("2222","点击");
Toast.makeText(MainActivity.this,"用户名与密码不能为空",Toast.LENGTH_SHORT);
new AlertDialog.Builder(MainActivity.this).
setTitle("☆ ☆ ☆").setMessage("账户名和密码不能为空").
setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
/**在此处以后应当再加入SQLite操作,将已注册的信息进行核实,未注册的不允许登录
Toast.makeText(MainActivity.this,"lalala",Toast.LENGTH_SHORT);
我也很绝望啊,Mate10的手机,打开权限看了允许通知,但是无法弹出Toast。。。我想了想,
弹出对话框得了,结果对话框也没给我弹出来,我这里只能看到log日志打印出来*/
else{
//我就先直接写登录跳转了
Intent mintent=new Intent(MainActivity.this,Activity_B.class);
//启动
//Log.d("3333","桥梁");
startActivity(mintent);
}
}
});
}
//记住密码了之后,还需要能够将其回显出来
private void setView() {
if (sharedPreferences == null)
sharedPreferences = getApplication().getSharedPreferences("config", MODE_PRIVATE);
editText.setText(sharedPreferences.getString("账号",""));
editTextb.setText(sharedPreferences.getString("密码",""));
mIsChecked=sharedPreferences.getBoolean("状态",false);
checkBox.setChecked(mIsChecked);
}
//这个函数用于调整为edittext设置的图标的大小还有记住密码的功能
private void setEdit() {
editText=findViewById(R.id.edittext1);
editTextb=findViewById(R.id.edittext2);
drawableb=getResources().getDrawable(R.drawable.icon_password);
drawable=getResources().getDrawable(R.drawable.icon_id);
drawable.setBounds(0,0,60,60);
drawableb.setBounds(0,0,60,60);
editTextb.setCompoundDrawables(drawableb,null,null,null);
editText.setCompoundDrawables(drawable,null,null,null);
//无论是要实现右下角字数计数器,还是登录功能实现,都需要对edittext进行输入内容的监听,不过重复的代码,
//大家最好封装一下吧,别这样反反复复的写
editText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//在文本改变之后记录用户账号
if (mIsChecked){
if (sharedPreferences == null)
sharedPreferences = getApplication().getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("账号", editText.getText().toString());
}
}
});
editTextb.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//在文本改变之后记录用户密码
if (mIsChecked){
if (sharedPreferences == null)
sharedPreferences = getApplication().getSharedPreferences("config", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("密码", editTextb.getText().toString());
}
}
});
checkBox=findViewById(R.id.checkbox);
//为记住密码的复选框设置监听,CompoundButton是一个具有选中和未选中状态的按钮,再被按下时改变状态
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//如果是选中状态
mIsChecked=isChecked;
if(isChecked){
/**这里用的sharedpreferance类,是一个轻量级的存储类,适合用于保存软件配置参数,
* 使用sharedpreferance存储数据,是用xml存放数据,如果希望存放的其他数据能够被
* 其他xml文件读和写,可以将getsharedpreferance中的MODE指定为可以指定
* Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。*/
if(sharedPreferences==null)
sharedPreferences= getApplication().getSharedPreferences("config",MODE_PRIVATE);
//获取sharedpreferences的编辑对象
SharedPreferences.Editor editor=sharedPreferences.edit();
//存储数据
// Log.d("11111", String.valueOf(editText.getText()));
editor.putString("账号",editText.getText().toString());
editor.putString("密码",editTextb.getText().toString());
editor.putBoolean("状态",isChecked);
/**进一步优化时当加入加密操作*/
//提交
editor.commit();
}
}
});
}
}
因为EditText默认的颜色不是我想要的,所以在styles文件里添加:
<style name="MyEditText" parent="Base.Theme.AppCompat.Light">
<item name="colorControlNormal">@android:color/tab_indicator_text</item>
<item name="colorControlActivated">@android:color/holo_orange_light</item>
</style>
注册一个新Activity
<activity android:name=".Activity_B"></activity>
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".Activity_B">
<ImageView
android:id="@+id/b_img"
android:src="@drawable/bg_btn"
android:layout_width="match_parent"
android:layout_height="match_parent" />
package com.example.lenovo.edittext_part;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
public class Activity_B extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Log.d("3333","桥梁");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
imageView=findViewById(R.id.b_img);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="@drawable/icon_id"
android:hint="邮箱或用户名"
android:paddingTop="80dp"
android:maxLength="8"
android:theme="@style/MyEditText" />
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="15"
android:drawableStart="@drawable/icon_password"
android:hint="密码"
android:inputType="textPassword"
android:theme="@style/MyEditText" />
<android.support.v7.widget.AppCompatCheckBox
android:text="记住密码"
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn_sign_in"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="80dp"
android:text="登录" />
</LinearLayout>
Demo下载地址:https://github.com/April6866/EditText_part