图片素材和源项目我放最下面的需要的可以自行下载
横屏设置我上一期说了,这里就不多做解释。
(3条消息) Android studio实现标题,状态栏隐藏和横屏(带图文,实现沉浸式体验)_将军府中来的博客-CSDN博客https://blog.csdn.net/qq_55985667/article/details/123698609?spm=1001.2014.3001.5502 如果能做出这个界面页面布局就差不多算入门了
大佬直接copy就行了,刚入门的小白的话我会在文章后面详细介绍一下,方便理解。
这里我就不多废话了,直接上代码
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"
android:background="@mipmap/bg0"
tools:context=".Register">
<RelativeLayout
android:id="@+id/re_one"
android:layout_width="350dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="@mipmap/re_one"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:layout_marginRight="25dp"
android:background="@color/white"
android:hint="请输入淘米账号"
android:textSize="20sp" />
<ImageView
android:id="@+id/preson"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignTop="@+id/et_number"
android:layout_marginRight="-30dp"
android:layout_toLeftOf="@+id/et_number"
android:background="@drawable/ic_baseline_person_24" />
<ImageView
android:id="@+id/password_one"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignTop="@+id/password_two"
android:layout_marginRight="-30dp"
android:layout_toLeftOf="@+id/et_number"
android:background="@drawable/ic_baseline_lock_24" />
<EditText
android:id="@+id/password_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_number"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="25dp"
android:background="@color/white"
android:hint="请输入密码"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/ll_one"
android:layout_alignLeft="@+id/password_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/password_two"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:layout_marginRight="25dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="75dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_registter"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="注册账号"
android:textColor="#00A8BF"
android:textSize="18sp" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_below="@+id/tv_registter"
android:layout_alignLeft="@+id/tv_registter"
android:layout_alignRight="@+id/tv_registter"
android:layout_marginTop="2dp"
android:background="#00A8BF" />
</LinearLayout>
<LinearLayout
android:layout_width="75dp"
android:layout_marginLeft="105dp"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/password_two"
android:orientation="vertical">
<TextView
android:id="@+id/tv_recall"
style="@style/recall" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_below="@+id/tv_recall"
android:layout_alignLeft="@+id/tv_recall"
android:layout_alignRight="@+id/tv_recall"
android:layout_marginTop="2dp"
android:background="#00A8BF" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="120dp"
android:layout_height="30dp"
android:layout_below="@+id/password_two"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:background="@mipmap/dl" />
</RelativeLayout>
<CheckBox
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:layout_marginLeft="250dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#204187"/>
<!-- android:layout_centerHorizontal="true"-->
</RelativeLayout>
Java设置:
package com.example.mygame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class Register extends AppCompatActivity implements View.OnClickListener {
private EditText etNumber;
private EditText etPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initView();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //显示状态栏
Map<String, String> userMap = FileSaveAccount.getUserInfo(Register.this);
if (userMap==null){
}else {
etPassword.setText(userMap.get("number"));
etNumber.setText(userMap.get("password"));
}
}
private void initView() {
etNumber = (EditText) findViewById(R.id.et_number);
etPassword = (EditText) findViewById(R.id.password_two);
btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String number = etNumber.getText().toString().trim();
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(number)) {
Toast.makeText(this, "请输入淘米账号", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登录成功" + number + "" + password, Toast.LENGTH_SHORT).show();
if (FileSaveAccount.saveUserInfo(Register.this, number, password)) {
Toast.makeText(this, "保存成功" + number + "" + password, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "保存失败" + number + "" + password, Toast.LENGTH_SHORT).show();
}
}
}
package com.example.mygame;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class FileSaveAccount {
public static boolean saveUserInfo(Context context, String number, String passwrod) {
try {
FileOutputStream fos = context.openFileOutput("data.txt", Context.MODE_PRIVATE);
fos.write((number + ":" + passwrod).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static Map<String, String> getUserInfo(Context context) {
String content = "";
try {
FileInputStream fis = context.openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
Map<String, String> userMap = new HashMap<String, String>();
String[] infos = content.split(":");
userMap.put("number", infos[0]);
userMap.put("password", infos[1]);
fis.close();
return userMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
AndroidManifest启动设置:
package com.example.mygame;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
public class FileSaveAccount {
public static boolean saveUserInfo(Context context, String number, String passwrod) {
try {
FileOutputStream fos = context.openFileOutput("data.txt", Context.MODE_PRIVATE);
fos.write((number + ":" + passwrod).getBytes());
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static Map<String, String> getUserInfo(Context context) {
String content = "";
try {
FileInputStream fis = context.openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
Map<String, String> userMap = new HashMap<String, String>();
String[] infos = content.split(":");
userMap.put("number", infos[0]);
userMap.put("password", infos[1]);
fis.close();
return userMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
布局的我之前做过几期了,这里也不废话,这里简单说一下新增加的控件(TextView,EditText,Button)
控件属性 | 功能描述 |
android:text | 设置文本 |
android:textColor | 设置文本颜色 |
android:textSize | 设置文本大小,推荐单位为sp |
android:height | 设置文本区域高度,支持单位:px/dp(推荐)/sp/in/mm |
android:width | 设置文本区域宽度,支持单位:px/dp(推荐)/sp/in/mm |
android:textStyle | 设置文字样式,如boid(粗体),italic(斜体) |
android:maxLenght | 设置文本长度,超出不显示 |
android:passwrod | 设置文本以密码形式“.”显示 |
android:gravity | 设置文本位置,如设置成“center”,文本将居中显示 |
android:phoneNumber | 设置以电话号码方式输入 |
android:layout_height | 设置TextView控件的高度 |
android:layout_width | 设置TextView控件的宽度 |
TextView控件其实还有很多属性,这里就不一一列举了
控件属性 | 功能描述 |
android:hint | 设置EditText没有输入内容时显示的提示文本 |
android:lines | 设置固定行数来决定EditText的高度 |
android:maxLines | 设置最大行数 |
android:minLines | 设置最小行数 |
android:password | 设置文本以密码的形式显示“.”显示 |
android:phoneNumber | 设置文本以电话号码方式输入 |
android:scrollHorizontally | 设置文本超过TextView的宽度的情况下,是否出现横拉条 |
android:capitalize | 设置首字母大写 |
android:editable | 设置是否可编辑 |
这里再多说一点,如果我们可以设置一个style通过id的调用,直接作用于多个控件这样会大大提升写代码的效率。Button(按钮)一般作用于响应用户的一系列点击事件,常用的点击方式有3种,分别是在布局中指定onClick属性、使用匿名内部类、在当前Activity中实现OnClickListener接口。
–文件存储是Android中最基本的一种数据存储方式,它与Java中的文件存储类似,都是通过I/O流的形式把数据存储到文档中。
这里我们是用内部存储存储账号密码的,内部储存--写入方法如下:
String fileName = "data.txt";
String content = "helloworld";
FileOutputStream fos;
try {
fos = openFileOutput(fileName, MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
内部储存--读取
String fileName = "data.txt";
String content = "helloworld";
FileOutputStream fos;
try {
fos = openFileOutput(fileName, MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
如果需要用到外部储存的外一般需要声明权限,这也是Android保证应用程序的安全性做了相关规定,如果程序需要访问系统的一些关键信息,必须要在清单文凭中声明权限才可以,否则程序运行时会直接崩溃。
由于操作SD卡中的数据属于系统中比较关键的信息,因此需要在清单文件的<manifest>节点中添加SD卡的读写权限
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
讲得本来只想将布局的因为刚好做了个功能,就刚好把内部储存讲了,代码篇幅确实有点长了,但对于Android初学者来说还是得多学多练,才能加深理解。
https://download.csdn.net/download/qq_55985667/85037159https://download.csdn.net/download/qq_55985667/85037159https://download.csdn.net/download/qq_55985667/85037168https://download.csdn.net/download/qq_55985667/85037168
第一个是图片,第二个是源项目,如果对你有帮组的话可以点个免费的赞么,谢谢!
内部存储—写入
内部存储—