写在前⾯
由于本⼈初学阶段,写这篇博客是总结所学的知识点,为后⾯的进阶打好基础
有任何关于代码和表述问题,欢迎评论区指出
楼主近期在学习关于安卓中Fragment和ListView中的知识,按照⽼师的要求模仿⼀下QQ界⾯ 要求功能有登录界⾯
密码不对提⽰密码不对
账号密码任⼀为空提⽰⽤户不能为空
登录成功提⽰登录成功
可以实现账号密码记住功能
有三个界⾯可以点击底部按钮实现页⾯的切换
实现按钮选中状态和未选中状态不⼀样
联系⼈界⾯
信息界⾯
状态界⾯
发送信息功能
点击信息界⾯中的任意消息可以进⼊发消息界⾯
可以实现点击发送按钮将所输⼊的⽂字显⽰在屏幕中
提⽰:本⼈⽤的IDE开发环境是Android Studio API是30
⽬录结构⼀览
res中的⽂件
1. 登录界⾯
登录界⾯的布局⽂件
l
所⽤图标都可以在阿⾥巴巴图标⽹到
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android="schemas.android/apk/res/android"
app="schemas.android/apk/res-auto"
tools="schemas.android/tools"
layout_width="match_parent"
layout_height="match_parent"
context=".MainActivity">
<LinearLayout
layout_width="match_parent"
layout_height="match_parent"
layout_marginLeft="10dp"
layout_marginRight="10dp"
orientation="vertical"
ignore="UselessParent">
<TextView
drawableLeft="@drawable/qq"
layout_width="wrap_content"
layout_height="wrap_content"
layout_marginTop="50dp"
layout_marginLeft="120dp"
gravity="center"
text="QQ"
textColor="#000000"
textSize="35sp"
drawableStartCompat="@drawable/qq"
drawableStart="@drawable/qq"
ignore="UseCompatTextViewDrawableXml"
layout_marginStart="120dp">
</TextView>
<EditText
id="@+id/userNameEditText"
layout_width="match_parent"
layout_height="wrap_content"
layout_marginTop="100dp"
hint="请输⼊⽤户名/账号/⼿机号"
inputType="text"
padding="5dp">
</EditText>
<EditText
id="@+id/passwordEditText"
layout_width="match_parent"
layout_height="wrap_content"
layout_marginTop="40dp"
hint="请输⼊密码"
inputType="textPassword"
padding="5dp">
</EditText>
<ImageButton
id="@+id/loginButton"
layout_marginTop="60dp"
layout_gravity="center"
layout_width="60dp"
layout_height="60dp"
src="@drawable/login"
background="#fff">
</ImageButton>
</LinearLayout>
</RelativeLayout>
所对应的MainActivity
其中的判断通过if语句判断⽤户是否输⼊和输⼊正确
提⽰ 通过Toast的⽅式提⽰
package;
import AppCompatActivity;
import SuppressLint;
import Context;
import Context;
import Intent;
import Bundle;
import View;
import Button;
import EditText;
import ImageButton;
import Toast;改密码qq
public class MainActivity extends AppCompatActivity {
private Context context;
private EditText userNameEditText, passwordEditText;
private ImageButton loginButton;
private ShareHelper shareHelper;
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
context=this;
shareHelper=new ShareHelper(context);
initview();
}
private void initview(){
userNameEditText =findViewById(R.id.userNameEditText);
passwordEditText =findViewById(R.id.passwordEditText);
loginButton =findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
//进⾏登录页⾯的处理
String username = Text().toString();
String password = Text().toString();
if(username.length()>0){
if(username.equals("zk")){
if(password.length()>0){
if(password.equals("123")){
// 对账号和密码进⾏保存
shareHelper.save("username",username);
shareHelper.save("password",password);
startActivity(new Intent(MainActivity.this, HomeActivity.class));
Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"密码不正确", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(MainActivity.this,"请填写密码",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(MainActivity.this,"⽤户名不正确",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(MainActivity.this,"请填写⽤户名",Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onStart(){
userNameEditText.ad("username"));
passwordEditText.ad("password"));
}
}
}
2. 记住密码功能
实现第⼀次登录成功后,再次登录会记住账号和密码的功能
这个功能我们⽤SharedPreferences
简单了解下什么是SharedPreferences
SharedPreferences是Android平台上⼀个轻量级的存储辅助类,⽤来保存应⽤的⼀些常⽤配置,它提供了
string,set,int,long,float,boolean六种数据类型。最终数据是以xml形式进⾏存储。在应⽤中通常做⼀些简单数据的持久化缓存。
package;
import Context;
import SharedPreferences;
public class ShareHelper {
//两个功能保存,读取
Context context;
public ShareHelper(){
}
public ShareHelper(Context context){
}
//保存
public void save(String key, String value){
SharedPreferences sharedPreferences = SharedPreferences("iot1921", Context.MODE_PRIVATE);
//创建⼀个输⼊值
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editormit();
}
//读取数据
public String read(String key){
SharedPreferences sharedPreferences = SharedPreferences("iot1921", Context.MODE_PRIVATE);
String(key,"");
}
}
注意事项:
输⼊值记得提交,editormit();
在MainACtivity中记得在OnStart⽅法中使⽤ad传⼊需要记住的值!
预览⼀下成果!
3. Fragment界⾯跳转
这⾥有三个Fragment,所对应需要三个fragment布局界⾯
⾸先要创建3个按钮的切换xml
<?xml version="1.0" encoding="utf-8"?>
<selector android="schemas.android/apk/res/android">
<item drawable="@drawable/message_on"state_selected="true"/> <item drawable="@drawable/message_off"state_selected="false"/> </selector>
<?xml version="1.0" encoding="utf-8"?>
<selector android="schemas.android/apk/res/android">
<item drawable="@drawable/people_on"state_selected="true"/>
<item drawable="@drawable/people_off"state_selected="false"/> </selector>
发布评论