这次做了一些关于登陆界面的基本布局的工作,登陆界面的功能的实现(包括注册、填写账号、密码、登陆等)将在接下来的工作中逐步实现。
首先是登陆界面的布局文件,包括背景、分别输入账号和密码的文本框、登陆按键、注册按键、:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/splash"
android:orientation="vertical">
<!--Android图片圆角类库,一个支持每个图片角不同半径圆角的Android ImageView-->
<com.joooonho.SelectableRoundedImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
app:sriv_left_top_corner_radius="20dp"
app:sriv_left_bottom_corner_radius="20dp"
app:sriv_right_bottom_corner_radius="20dp"
app:sriv_right_top_corner_radius="20dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="58dp"
android:src="@mipmap/app_logo_white"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@mipmap/icon_user_account" />
<EditText
android:id="@+id/et_user_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@null"
android:hint="请输入账号"
android:digits="@string/digits"
android:inputType="textEmailAddress"
android:textColor="@android:color/white"
android:textColorHint="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="7dp"
android:background="@color/hint_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_marginLeft="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@mipmap/icon_pass_word" />
<EditText
android:id="@+id/et_pass_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@null"
android:hint="请输入密码"
android:digits="@string/digits"
android:inputType="textPassword"
android:textColor="@android:color/white"
android:textColorHint="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="7dp"
android:background="@color/hint_color" />
<TextView
android:id="@+id/tv_btn_login"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="46dp"
android:gravity="center"
android:background="@drawable/login_nol_bg"
android:text="@string/login"
android:textColor="#1A8ECB"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="4dp"
android:gravity="center"
android:paddingTop="20dp"
android:text="@string/register_tip"
android:textColor="@color/white_color"
android:textSize="14sp"/>
<View
android:layout_width="120dp"
android:layout_height="0.8dp"
android:layout_gravity="center_horizontal"
android:background="@color/white_color" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="6dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:textColor="@color/app_blue_color"
android:text="@string/app_version"/>
</RelativeLayout>
</LinearLayout>
聊天通信通过借助网易云通信来实现,在gradle中添加一下代码请求这些功能
// 基础功能 (必需)
compile 'com.netease.nimlib:basesdk:3.3.0'
// 音视频需要
compile 'com.netease.nimlib:avchat:3.3.0'
// 全文检索服务需要
compile 'com.netease.nimlib:lucene:3.3.0'
导入在文件中导入相关的库
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
分配一个数组用来存储各种需要申请的权限(sd卡写的权限、相机权限、读取手机状态数据的权限、录音权限、获取粗略位置(基站服务信号)和精确位置(GPS)的权限)
private final String[] BASIC_PERMISSIONS = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
};
检查、申请权限的方法
private void initPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
boolean has = PermissionUtils.checkPermissions(this, BASIC_PERMISSIONS);
if (!has){
PermissionUtils.requestPermissions(this,PERMISSION_REQUEST_CODE,BASIC_PERMISSIONS);
}
}
跳转到权限设置页面返回后再次检查
protected void onRestart() {
super.onRestart();
initPermission();
}
权限授予结果回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE){
PermissionUtils.dealPermissionResult(LoginActivity.this, permissions, grantResults,
new PermissionUtils.RequestPermissionCallBack() {
@Override
public void onGrant(String... permissions) {
}
@Override
public void onDenied(String... permissions) {
}
@Override
public void onDeniedAndNeverAsk(String... permissions) {
}
});
}
}
Activity的OnCreate方法:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
mEtPassWord.setOnTouchListener(new MyTouchListener());
initPermission();
}
因为日后需要保存登陆信息,所以这里自定义一个SharedPreferencesUtil通过SharedPreferences提供的java常规的Long、Int、String等类型数据的保存接口来存储登陆信息
public static void setIntSharedPreferences(Context context, String name, String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
public static void setStringSharedPreferences(Context context, String name, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
if (sharedPreferences.contains(key)) {
editor.remove(key);
editor.commit();
}
editor.putString(key, value);
editor.commit();
}
public static int getIntSharedPreferences(Context context, String name, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Activity.MODE_PRIVATE);
int value = sharedPreferences.getInt(key, 0);
return value;
}
public static String getStringSharedPreferences(Context context, String name, String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Activity.MODE_PRIVATE);
String value = sharedPreferences.getString(key, "");
return value;
}
在日后实现登陆及其它的一些功能时会用到这个类中的方法。