1.build.gradle文件配置
applyplugin:'com.android.application'
android{
compileSdkVersion29
buildToolsVersion"29.0.3"
defaultConfig{
applicationId"com.chai.welcome"
minSdkVersion16
targetSdkVersion29
versionCode1
versionName"1.0"
testInstrumentationRunner"androidx.test.runner.AndroidJUnitRunner"
}
buildTypes{
release{
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
//Invoke-customs are only supported starting with Android O (--min-api 26)
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies{
implementation fileTree(dir:"libs",include: ["*.jar"])
implementation'androidx.appcompat:appcompat:1.2.0'
implementation'com.google.android.material:material:1.2.1'
implementation'androidx.constraintlayout:constraintlayout:2.0.1'
implementation'androidx.navigation:navigation-fragment:2.3.0'
implementation'androidx.navigation:navigation-ui:2.3.0'
testImplementation'junit:junit:4.13'
androidTestImplementation'androidx.test.ext:junit:1.1.2'
androidTestImplementation'androidx.test.espresso:espresso-core:3.3.0'
//网络请求库
implementation'com.squareup.retrofit2:retrofit:2.9.0'
implementation'com.squareup.retrofit2:retrofit-converters:2.8.1'
implementation'com.squareup.retrofit2:converter-gson:2.9.0'
implementation'com.squareup.okhttp3:logging-interceptor:4.8.1'
implementation'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
//函数响应式 生命周期库
implementation'com.trello.rxlifecycle2:rxlifecycle:2.2.2'
implementation'com.trello.rxlifecycle2:rxlifecycle-components:2.2.2'
//多标签
//日志库
implementation'com.orhanobut:logger:2.2.0'
//view数据绑定
implementation'com.jakewharton:butterknife:10.2.3'
annotationProcessor'com.jakewharton:butterknife-compiler:10.2.3'
//加载图片
implementation'com.github.bumptech.glide:glide:4.11.0'
//导航栏
implementation'com.android.support:design:29.+'
//数据库
implementation'org.xutils:xutils:3.9.0'
//事件发布-订阅总线
implementation'org.greenrobot:eventbus:3.1.1'
}
2.BaseActivity.java
public abstract class BaseActivityextends RxAppCompatActivity {
public static final int MATCH_PARENT = LinearLayout.LayoutParams.MATCH_PARENT;
public static final int WRAP_CONTENT = LinearLayout.LayoutParams.WRAP_CONTENT;
public static int displayWidth =0;
public static int displayHeight =0;
public static float density =0;
public Contextcontext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
density = getResources().getDisplayMetrics().density;
context =this;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
displayWidth = displayMetrics.widthPixels;
displayHeight = displayMetrics.heightPixels;
}
public Drawable getMyDrawable(@DrawableRes int id) {
return ContextCompat.getDrawable(context, id);
}
public int getMyColor(@ColorRes int id) {
return ContextCompat.getColor(context, id);
}
public RequestBody createRequestBody(List> list){
return FormBody.create(new Gson().toJson(list),
MediaType.parse("application/json; charset=utf-8"));
}
public RequestBody createRequestBody(Map paramMap){
return FormBody.create(new Gson().toJson(paramMap),
MediaType.parse("application/json; charset=utf-8"));
}
public void toSubscribeSingleOb(Observable ob) {
ob.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(this.bindUntilEvent(ActivityEvent.DESTROY));
}
//android点击屏幕上EditText区域以外的任何地方隐藏键盘的方法
public Boolean hideInputMethod(Context context, View v) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm !=null) {
return imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
return false;
}
//判断当前点击屏幕的地方是否是软键盘:
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v !=null && (vinstanceof EditText)) {
int[] leftTop = {0,0 };
v.getLocationInWindow(leftTop);
int left = leftTop[0], top = leftTop[1], bottom = top + v.getHeight(), right = left
+ v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
// 保留点击EditText的事件
return false;
}else {
return true;
}
}
return false;
}
//覆写activity的点击事件的分发方法dispatchTouchEvent:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
if(hideInputMethod(this, v)) {
return true;//隐藏键盘时,其他控件不响应点击事件==》注释则不拦截点击事件
}
}
}
return super.dispatchTouchEvent(ev);
}
}
3.新建WelcomeActivity.java
public class WelcomeActivityextends BaseActivity {
@BindView(R.id.tv_tips)
TextViewtv_tips;
public static int COUNT =3;
public Handlerhandler =new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0x01:
tv_tips.setText(COUNT+"S后跳转");
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
ButterKnife.bind(this);
//开始倒计时
startCountdown();
}
//开始倒计时
private void startCountdown(){
COUNT =3;
final Timer timer =new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(COUNT >0){
Message message = Message.obtain();
message.what =0x01;
handler.sendMessage(message);
}else {
Intent intent =new Intent();
intent.setClass(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
timer.cancel();
}
COUNT--;
}
},2000,1000);
}
}
4.新建activity_welcome.xml布局文件
android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/welcome"> android:id="@+id/tv_tips" android:layout_width="wrap_content" android:layout_height="@dimen/dp_20" android:layout_alignParentRight="true" android:layout_margin="@dimen/dp_15" android:paddingLeft="@dimen/dp_5" android:paddingRight="@dimen/dp_5" android:text="3S后跳转" android:textColor="@color/_white" />
5.配置 AndroidManifest.xml
package="com.chai.welcome"> android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> android:screenOrientation="portrait"> android:screenOrientation="portrait"/> android:name=".MainActivity" android:screenOrientation="portrait">
6.在style.xml 中设置无主题样式
7.在color.xml中设置倒计时字体颜色样式