上次我们做了app启动时的延时页,这次我们来做登陆注册的页面。
xml代码:
<?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"
tools:context=".MainActivity"
android:background="@drawable/login"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/login"
android:layout_marginTop="170dp"
android:layout_gravity="center_horizontal"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_green"
android:text="登 录"
android:textColor="#FFFFFF"
android:textSize="22sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/register"
android:layout_marginTop="170dp"
android:textSize="22sp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="注 册"
android:textColor="#FFFFFF"
android:layout_width="140dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Acticity类中只填加了两个按钮的点击监听事件,在这里不写了。
顺便说一下,我做的按钮样式,如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色 -->
<solid android:color="#49bbf7" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="13dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
下面是注册的页面
一样的,我来写一下我的输入框样式设计代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#80b9b5aa" />
<stroke android:width="1dip" android:color="#fefefe" />
<corners android:radius="20dp"/>
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
注册页面xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".LoginActivity"
android:background="@drawable/login">
<EditText
android:id="@+id/phone_input"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phonetext"
android:layout_centerVertical="true"
android:background="@drawable/shape_input"
android:textSize="20sp"
/>
<EditText
android:layout_marginTop="37dp"
android:id="@+id/password_input"
android:gravity="center"
android:layout_marginRight="15dp"
android:background="@drawable/shape_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword"
android:textSize="20sp" android:layout_below="@+id/phone_input"
android:layout_alignStart="@+id/phone_input"/>
<Button
android:id="@+id/btn_login"
android:layout_marginTop="60dp"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注 册"
android:textColor="#FFFFFF"
android:layout_below="@id/password_input"
android:layout_centerHorizontal="true"
android:background="@drawable/shape"/>
</RelativeLayout>
acticity代码:
public class RegisterActivity extends Activity {
private EditText phone, password;
private Button reg;
//声明一个SharedPreferences对象和一个Editor对象
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
phone = (EditText) findViewById(R.id.phone_input);
password = (EditText) findViewById(R.id.password_input);
reg = (Button) findViewById(R.id.btn_login);
//获取preferences和editor对象
preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
editor = preferences.edit();
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = phone.getText().toString().trim();
String passwords = password.getText().toString().trim();
if(name!=null){
if(name.length()!=11){
Toast.makeText(getApplicationContext(),"手机号格式错误",Toast.LENGTH_SHORT).show();
}else {
if (passwords != null) {
editor.putString("userPhone", name);
editor.putString("userPassword", passwords);
editor.apply();
try {
saveFile(name + "@" + passwords);
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
RegisterActivity.this.finish();
} else {
//密码健壮性
Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
}
}
}else{
//手机号健壮性
Toast.makeText(getApplicationContext(),"手机号不能为空",Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveFile(String str) throws Exception {
try{
String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath();//获取SDCard目录
File dir = new File(sdCardDir + "/" + getPackageName());
if(!dir.exists())
{
if(!dir.mkdirs())
throw new Exception("failure");
}
File saveFile = new File(dir,"userInfo.txt");
if(!saveFile.exists())
if(!saveFile.createNewFile())
throw new Exception("failure");
FileOutputStream outStream = new FileOutputStream(saveFile,true);
outStream.write((str+"\r\n").getBytes());
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:这里我们用了SharedPreferences对象,作用呢,就是储存当前注册所输入的文本,存储到sharedPreferences中,我们直接把它初始化到登录页面的输入框中,这样就不用用户重新输入了。
还有就是文件储存用户信息的代码,我们首先写入读写权限,上一篇有提到。Environment.getExternalStorageDirectory().getAbsolutePath();是获取到sd卡的根目录,一般的设备都有sd卡,我们利用包名来命名创建一个文件夹,创建一个txt来储存用户信息。