需求提出
在用户信息的填写中,这个hint其实可以详细一点或者干脆不要,个人感觉有点怪怪的。但是说不出来是什么怪怪。
在实际的Demo中在这里进行注释解析,还算ok。个人认为UI上没必要强调注释类吧。随便了这一点。文章的主要目的是审阅代码学习,以上并不是特重点。
实际解析
UI设计
采用XMl,基本的布局是采用比较旧的RelativeLayout
相对布局
<?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="#fff">
<LinearLayout
android:id="@+id/modify_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginEnd="19dp"
android:layout_toStartOf="@id/language_area"
android:orientation="horizontal"
tools:visibility="visible">
<ImageView
android:background="@drawable/demo_login_theme_icon_light"
android:layout_gravity="center"
android:layout_width="17.28dp"
android:layout_height="17.28dp"/>
<TextView
android:id="@+id/demo_login_theme"
android:text="@string/modify_theme"
android:layout_marginLeft="5.7dp"
android:layout_marginRight="5.7dp"
android:textSize="13.44sp"
android:textColor="@color/demo_login_theme_language_text_color_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:background="@drawable/demo_login_language_arrow_light"
android:layout_gravity="center"
android:layout_width="9.6dp"
android:layout_height="9.6dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/language_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="50dp"
android:layout_marginEnd="28dp"
android:orientation="horizontal"
tools:visibility="visible">
<ImageView
android:layout_width="17.28dp"
android:layout_height="17.28dp"
android:layout_gravity="center"
android:background="@drawable/demo_login_language_icon_light" />
<TextView
android:id="@+id/demo_login_language"
android:text="@string/demo_language"
android:layout_marginLeft="5.7dp"
android:layout_marginRight="5.7dp"
android:textSize="13.44sp"
android:textColor="@color/demo_login_theme_language_text_color_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="9.6dp"
android:layout_height="9.6dp"
android:layout_gravity="center"
android:background="@drawable/demo_login_language_arrow_light" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<ImageView
android:id="@+id/logo"
android:layout_width="200dp"
android:layout_height="125dp"
android:layout_gravity="center_horizontal"
android:background="?attr/demo_login_ic_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/account"
android:textColor="#000"
android:textSize="16sp" />
<EditText
android:id="@+id/login_user"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/account_hint"
android:singleLine="true"
android:textCursorDrawable="@drawable/my_cursor"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/item_split_color" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/item_split_color" />
<TextView
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="44.16dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="28.8dp"
android:layout_marginRight="6dp"
android:background="@drawable/button_border"
android:gravity="center"
android:text="@string/button_login"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
布局的效果如图所示:
UI重点解析
其中的重点是布局的格式,回顾相对布局和线性布局的知识要点。这里需要对布局的位置layout、gravity等等要有一个比较清晰的认识和理解。
<ImageView
android:id="@+id/logo"
android:layout_width="200dp"
android:layout_height="125dp"
android:layout_gravity="center_horizontal"
android:background="?attr/demo_login_ic_logo" />
这里有个值得好好研究的地方。采用属性的方式匹配
?attr/demo_login_ic_logo
如下图所示:使用属性的方式,这个是对整个不同的UI风格适配的理想方法之一。在该IM项目中采用Attr方式存取一些统一的属性根据不同的style可以进行适配,实现UI上的动态变化。
在不同的style
中特指的item
就会不一样。以项目logo为例。不同的style就专门指向不同的drawable
。对于多种样式的适配,我觉得TIM的实现很值得学习和深入研究。
初步的思路就是通过根据本Activity中的theme选择解析适配,具体应该是要查看TUIThemeManager
现在先放放该对象,待后续再深入研究这个对象到底是如何实现的。将其视为透明的即可。重点回归到Activity的逻辑实现。
这里的特点feature就是采用广播的方式处理皮肤样式和语言的切换。
在这一点中就得回归到广播的练习中去。android-第一行代码-第五章 广播broadcast(温故而知新)学习记录
Activity完整源码
这里放置一些完整的源码,可以跳过这一小节
package com.tencent.qcloud.tim.demo.login;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.qcloud.tim.demo.DemoApplication;
import com.tencent.qcloud.tim.demo.R;
import com.tencent.qcloud.tim.demo.bean.UserInfo;
import com.tencent.qcloud.tim.demo.main.MainActivity;
import com.tencent.qcloud.tim.demo.signature.GenerateTestUserSig;
import com.tencent.qcloud.tim.demo.utils.DemoLog;
import com.tencent.qcloud.tim.demo.utils.TUIUtils;
import com.tencent.qcloud.tuicore.TUIThemeManager;
import com.tencent.qcloud.tuicore.component.activities.BaseLightActivity;
import com.tencent.qcloud.tuicore.util.ToastUtil;
/**
* <p>
* Demo的登录Activity
* 用户名可以是任意非空字符,但是前提需要按照下面文档修改代码里的 SDKAPPID 与 PRIVATEKEY
* https://github.com/tencentyun/TIMSDK/tree/master/Android
* <p>
*/
/**
* Described by Chris Wang
* 这个对登录用户中的信息进行Update,刷新
*/
public class LoginForDevActivity extends BaseLightActivity {
private static final String TAG = LoginForDevActivity.class.getSimpleName();
private TextView mLoginView;
private EditText mUserAccount;
private TextView languageTv;
private View languageArea;
private View modifyTheme;
private ImageView logo;
//使用广播的效果,详细观看官方的文档,回忆一下,知识都有些忘了
private BroadcastReceiver languageChangedReceiver;
private BroadcastReceiver themeChangedReceiver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//语言切换
languageChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
initActivity();
}
};
//样式切换
themeChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setCurrentTheme();
}
};
//广播核心注册并通过
IntentFilter languageFilter = new IntentFilter();
IntentFilter themeFilter = new IntentFilter();
languageFilter.addAction(LanguageSelectActivity.DEMO_LANGUAGE_CHANGED_ACTION);
themeFilter.addAction(ThemeSelectActivity.DEMO_THEME_CHANGED_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(languageChangedReceiver, languageFilter);
LocalBroadcastManager.getInstance(this).registerReceiver(themeChangedReceiver, themeFilter);
//初始化活动
initActivity();
}
private void initActivity() {
//确认Activity并包含UI的id
setContentView(R.layout.login_for_dev_activity);
languageArea = findViewById(R.id.language_area);
languageTv = findViewById(R.id.demo_login_language);
modifyTheme = findViewById(R.id.modify_theme);
logo = findViewById(R.id.logo);
//统一样式而已,基本在TIM中都是采用这种方式实现样式的统一。
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().setStatusBarColor(Color.TRANSPARENT);
getWindow().setNavigationBarColor(Color.TRANSPARENT);
}
mLoginView = findViewById(R.id.login_btn);
// 用户名可以是任意非空字符,但是前提需要按照下面文档修改代码里的 SDKAPPID 与 PRIVATEKEY
// https://github.com/tencentyun/TIMSDK/tree/master/Android
mUserAccount = findViewById(R.id.login_user);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
mLoginView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//App初始化
DemoApplication.instance().init(0);
//关键的的用户信息在单例的作用下进行缓存
UserInfo.getInstance().setUserId(mUserAccount.getText().toString());
// 获取userSig函数
String userSig = GenerateTestUserSig.genTestUserSig(mUserAccount.getText().toString());
UserInfo.getInstance().setUserSig(userSig);
//登录工具,TIM进行一个集合式的core类进行操作
TUIUtils.login(mUserAccount.getText().toString(), userSig, new V2TIMCallback() {
@Override
public void onError(final int code, final String desc) {
runOnUiThread(new Runnable() {
public void run() {
ToastUtil.toastLongMessage(getString(R.string.failed_login_tip) + ", errCode = " + code + ", errInfo = " + desc);
}
});
DemoLog.i(TAG, "imLogin errorCode = " + code + ", errorInfo = " + desc);
}
@Override
public void onSuccess() {
UserInfo.getInstance().setAutoLogin(true);
UserInfo.getInstance().setDebugLogin(true);
Intent intent = new Intent(LoginForDevActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
});
//编辑文本监听,常用的按钮可用与否的监听方式
mUserAccount.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 (TextUtils.isEmpty(mUserAccount.getText())) {
mLoginView.setEnabled(false);
} else {
mLoginView.setEnabled(true);
}
}
});
mUserAccount.setText(UserInfo.getInstance().getUserId());
//语言选择
languageArea.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LanguageSelectActivity.startSelectLanguage(LoginForDevActivity.this);
}
});
//皮肤样式选择按钮监听
modifyTheme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ThemeSelectActivity.startSelectTheme(LoginForDevActivity.this);
}
});
}
private void setCurrentTheme() {
//单例模式下进行样式管理的获取,在该界面对logo依据进行UI的更换
int currentTheme = TUIThemeManager.getInstance().getCurrentTheme();
if (currentTheme == TUIThemeManager.THEME_LIGHT) {
logo.setBackgroundResource(R.drawable.demo_ic_logo_light);
mLoginView.setBackgroundResource(R.drawable.button_border_light);
} else if (currentTheme == TUIThemeManager.THEME_LIVELY) {
logo.setBackgroundResource(R.drawable.demo_ic_logo_lively);
mLoginView.setBackgroundResource(R.drawable.button_border_lively);
} else if (currentTheme == TUIThemeManager.THEME_SERIOUS) {
logo.setBackgroundResource(R.drawable.demo_ic_logo_serious);
mLoginView.setBackgroundResource(R.drawable.button_border_serious);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
//对广播进行注销
super.onDestroy();
if (themeChangedReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(themeChangedReceiver);
themeChangedReceiver = null;
}
if (languageChangedReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(languageChangedReceiver);
languageChangedReceiver = null;
}
}
}
广播应用
- 构建几个关键对象,对广播进行接收。
//使用广播的效果,详细观看官方的文档,回忆一下,知识都有些忘了
private BroadcastReceiver languageChangedReceiver;
private BroadcastReceiver themeChangedReceiver;
- 对广播进行注册和监听
//语言切换
languageChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
initActivity();
}
};
//样式切换
themeChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setCurrentTheme();
}
};
//广播核心注册并通过单例进行注册
IntentFilter languageFilter = new IntentFilter();
IntentFilter themeFilter = new IntentFilter();
languageFilter.addAction(LanguageSelectActivity.DEMO_LANGUAGE_CHANGED_ACTION);
themeFilter.addAction(ThemeSelectActivity.DEMO_THEME_CHANGED_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(languageChangedReceiver, languageFilter);
LocalBroadcastManager.getInstance(this).registerReceiver(themeChangedReceiver, themeFilter);
这里LocalBroadcastManager.getInstance(this).unregisterReceiver
和直接在Activity中常用的unregisterReceiver()
有啥差嘛?
LocalBroadcastManager 与 Context 的 registerReceiver / sendBroadcast / unregisterReceiver 方法的区别解析
最终还是回归到安全性,就不用activity自带的方法来操作了。
- 以语言切换为例实现,在独立的语言Activity中会激发一个Intent进行广播操作。
private void notifyLanguageChanged() {
Intent intent = new Intent();
intent.setAction(DEMO_LANGUAGE_CHANGED_ACTION);
intent.putExtra(LANGUAGE, currentLanguage);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
这时候切换语言之类到时会激发本Activity的广播接收方法。
- 记得注销广播,不要浪费资源导致不必要的东西出现。
@Override
protected void onDestroy() {
//对广播进行注销
super.onDestroy();
if (themeChangedReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(themeChangedReceiver);
themeChangedReceiver = null;
}
if (languageChangedReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(languageChangedReceiver);
languageChangedReceiver = null;
}
}
广播实现效果