Skip to content

Commit

Permalink
ToastUtil适配Android 11
Browse files Browse the repository at this point in the history
  • Loading branch information
YeChao committed Jan 26, 2021
1 parent 35eeb26 commit 3f7cc17
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 53 deletions.
18 changes: 10 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
compileSdkVersion 30

defaultConfig {
applicationId "com.yechaoa.app"
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

buildTypes {
Expand All @@ -26,11 +26,13 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation "com.google.android.material:material:1.2.1"

implementation project(':yutils')
implementation project(':yutilskt')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.yechaoa.app

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/yechaoa/app/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.yechaoa.app

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar
import com.yechaoa.yutilskt.*
import kotlinx.android.synthetic.main.activity_main.*

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.61'
ext.kotlin_version = '1.4.21'

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:4.0.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Tue Jan 26 17:54:05 CST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
12 changes: 6 additions & 6 deletions yutils/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28
compileSdkVersion 30
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
Expand All @@ -19,9 +19,9 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit:junit:4.12'
implementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'junit:junit:4.13'
implementation 'com.google.code.gson:gson:2.8.6'
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.yechaoa.yutils;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
11 changes: 3 additions & 8 deletions yutils/src/main/java/com/yechaoa/yutils/ToastUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.yechaoa.yutils;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
Expand Down Expand Up @@ -59,15 +58,13 @@ public void run() {
*
* @param msg 接收参数
*/
@SuppressLint("ShowToast")
private static void createToast(String msg) {
if (toast == null) {
toast = Toast.makeText(YUtils.getApp().getApplicationContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
LinearLayout linearLayout = (LinearLayout) toast.getView();
TextView messageTextView = (TextView) linearLayout.getChildAt(0);
messageTextView.setTextSize(15);
toast.show();
}

Expand Down Expand Up @@ -113,16 +110,14 @@ public void run() {
*
* @param msg 接收参数
*/
@SuppressLint("ShowToast")
private static void createCenterToast(String msg) {
if (toast == null) {
toast = Toast.makeText(YUtils.getApp().getApplicationContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
LinearLayout linearLayout = (LinearLayout) toast.getView();
TextView messageTextView = (TextView) linearLayout.getChildAt(0);
toast.setGravity(Gravity.CENTER, 0, 0);
messageTextView.setTextSize(15);
toast.show();
}

Expand Down
14 changes: 7 additions & 7 deletions yutilskt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
compileSdkVersion 30

defaultConfig {
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
consumerProguardFiles 'consumer-rules.pro'
}

Expand All @@ -26,8 +26,8 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.yechaoa.yutilskt

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith
Expand Down
3 changes: 1 addition & 2 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ActivityUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,11 @@ object ActivityUtil {
activityStack.remove(activity)
activityStack.push(activity)
}

override fun onActivityStarted(activity: Activity) {}
override fun onActivityResumed(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {
activityStack.remove(activity)
}
Expand Down
2 changes: 1 addition & 1 deletion yutilskt/src/main/java/com/yechaoa/yutilskt/DisplayUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object DisplayUtil {
*/
fun getActionBarHeight(): Int {
val tv = TypedValue()
return if (YUtils.getApp().theme.resolveAttribute(R.attr.actionBarSize, tv, true)) {
return if (YUtils.getApp().theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
TypedValue.complexToDimensionPixelSize(tv.data, YUtils.getApp().resources.displayMetrics)
} else 0
}
Expand Down
2 changes: 1 addition & 1 deletion yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object SpUtil {
}

fun getString(key: String?, defValue: String = ""): String {
return sp.getString(key, defValue)
return sp.getString(key, defValue)!!
}

/**
Expand Down
11 changes: 3 additions & 8 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.yechaoa.yutilskt

import android.annotation.SuppressLint
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast

/**
Expand Down Expand Up @@ -42,15 +41,13 @@ object ToastUtil {
*
* @param msg 接收参数
*/
@SuppressLint("ShowToast")
private fun createToast(msg: String) {
if (toast == null) {
toast = Toast.makeText(YUtils.getApp().applicationContext, msg, Toast.LENGTH_SHORT)
} else {
toast!!.setText(msg)
}
val linearLayout = toast!!.view as LinearLayout
val messageTextView = linearLayout.getChildAt(0) as TextView
messageTextView.textSize = 15f
toast!!.show()
}

Expand Down Expand Up @@ -81,16 +78,14 @@ object ToastUtil {
*
* @param msg 接收参数
*/
@SuppressLint("ShowToast")
private fun createCenterToast(msg: String) {
if (toast == null) {
toast = Toast.makeText(YUtils.getApp().applicationContext, msg, Toast.LENGTH_SHORT)
} else {
toast!!.setText(msg)
}
val linearLayout = toast!!.view as LinearLayout
val messageTextView = linearLayout.getChildAt(0) as TextView
toast!!.setGravity(Gravity.CENTER, 0, 0)
messageTextView.textSize = 15f
toast!!.show()
}

Expand Down
2 changes: 1 addition & 1 deletion yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ object YUtils {
*/
fun copyToClipboard(text: String?) {
val cm = getApp().getSystemService(Activity.CLIPBOARD_SERVICE) as ClipboardManager
cm.primaryClip = ClipData.newPlainText(getApp().packageName, text)
cm.setPrimaryClip(ClipData.newPlainText(getApp().packageName, text))
}

/**
Expand Down

0 comments on commit 3f7cc17

Please sign in to comment.