Tag: 安卓

匕首注入超类和子类

BaseFragment类 open class BaseFragment : Fragment() { @Inject lateinit var apiManager: ApiManager @Inject lateinit var eventBus: EventBus override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) App.getInstance().component.inject(this) } override fun onStop() { eventBus.unreg(this) super.onStop() } override fun onStart() { super.onStart() eventBus.reg(this) } } RoomsFragment类 class RoomsFragment : BaseFragment() { @Inject lateinit var roomAdapter: RoomsAdapter override fun onCreate(savedInstanceState: Bundle?) […]

创建一个微调器,当选择一个项目时调整它的宽度

我有一个微调,我想它的宽度是用户选择的项目的宽度。 当试图获得在两个不同的设备上的文字宽度,我得到两个不同的结果。 要获得文本宽度,我正在做以下事情: fun textDim(text: String): Int { val bounds = Rect() val textPaint = Paint() textPaint.textSize = 16f textPaint.getTextBounds(text, 0, text.length, bounds) return bounds.width() } 但是,这不起作用,并且不能在两台设备上正确缩放。 所以,我试了一下 fun textDim(text: String, context: Context): Int { val bounds = Rect() val textPaint = Paint() textPaint.textSize = 16f textPaint.getTextBounds(text, 0, text.length, bounds) return bounds.width() * context.resources.displayMetrics.density.toInt() } […]

格式@Query参数 – 改造2

我切换到使用Retrofit2,一切都很好,干净…但有一个服务调用,要求在正则表达式内查询参数(不要问我为什么,我已经要求它被改变)。 以下是我的方法: @GET("prod/search") fun searchProducts(@Query("q") q: String?): Call<Array<Product>> 如果我调用searchProducts("lala") ,查询将如下所示: prod/search?q=lala ,但是我需要它看起来像prod/search?q=(?i)\\Qlala\\E 有一种简单的方法来格式化查询参数来做到这一点?

如何保存从RadioButton中选择的颜色,并使用android应用于ListView

这是我的addContact类,它允许用户添加新的联系人并将其保存在Sqlite数据库中 姓名和电话已经保存在数据库中,但是颜色不会被应用到主类的listView中 在这里输入图像说明 我尝试隐藏textView,一旦用户选择一个选项,然后它与选定的值比较,但不起作用。 我的问题是如何选择一个RadioButton和颜色将适用于主要使用ListView 谢谢。

java.lang.ClassNotFoundException:com.intellij.junit5.JUnit5IdeaTestRunner在kotlin中使用spek

我在kotlin测试中使用spek非常新。 使用spek时,在logcat上得到以下错误。 我不是为什么我得到这个 java.lang.ClassNotFoundException: com.intellij.junit5.JUnit5IdeaTestRunner at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.intellij.rt.execution.junit.JUnitStarter.getAgentClass(JUnitStarter.java:252) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131) 我有以下依赖gradle文件 testImplementation 'junit:junit:4.12' testImplementation 'org.jetbrains.spek:spek-api:1.1.0-beta3' testImplementation 'org.jetbrains.spek:spek-junit-platform-engine:1.1.0-beta3' testImplementation 'org.junit.platform:junit-platform-runner:1.0.0-M3' 测试文件 import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.given import org.jetbrains.spek.api.dsl.it import org.junit.Assert.assertEquals import org.junit.platform.runner.JUnitPlatform […]

在Dagger 2中提供方法

我有问题,我不知道为什么,但我的变量不注入和等于null。 我提供这样的方法: @Provides @Singleton fun provideSource(context: Context): DatabaseSource = DatabaseSource(context, Models.DEFAULT, DB_NAME, DB_VERSION) @Provides @Singleton fun provideStoreDB(databaseSource: DatabaseSource): KotlinReactiveEntityStore<Store> = KotlinReactiveEntityStore(KotlinEntityDataStore(databaseSource.configuration)) @Provides @Singleton @NotNull fun provideStores() : Database<Store> = PersistentDatabase(Store::class) 和模特商店的类: 它是“持久”阶级的继承人。 @Entity data class Store(val id: Long = 0, val name: String = "", val description: String = "", val date: String = "") […]

在android studio 3.0 canary 4 ui预览中丢失的类

我得到了我的XML预览器中缺少类的错误。 所以我不能在预览中看到我的布局,但是当我运行应用程序时,我可以看到一切正常。 这些类是在我的android studio项目中的一个模块内,并包含如下: dependencies { compile project(":styleguide") } 并用于这样的XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="64dp" android:orientation="vertical"> <com.company.packagename.buttons.SecondaryButtonLink android:layout_height="wrap_content" android:layout_width="match_parent"/> </LinearLayout> 按钮看起来像这样: <merge xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/base_secondary_button_link_ll" android:layout_width="match_parent" android:layout_height="@dimen/button_height" android:clickable="true" style="@style/SelectableItemBackground" android:orientation="horizontal"> <com.company.packagename.textviews.LabelTextView android:id="@+id/secondary_button_link_title_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="@drawable/selector_secondary_button_link" android:drawableRight="@drawable/arrow_light_blue" android:drawablePadding="@dimen/button_icon_left_margin"/> </merge> 而按钮类背后的代码是这样的: class SecondaryButtonLink @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) […]

在Android库中显示类选择器

我正在创建一个库,我想创建一个类选择器。 当一个编辑文本被点击时,它会显示一个对话框,其中我将显示当前应用程序中的所有包和类。 用户可以从中选择一个班级。 我尝试了一些挣扎了2个小时。 但无法弄清楚如何做到这一点。 我看到这个答案使用DexFile 。 不过,从API 26开始已经被弃用了。

Android ValueAnimator仅增加高度

使用我的下面的代码规模增加两个方面的高度。 我想增加一个方向的高度 private fun scaleView(view: View, newHeight: Int) { val animation : ValueAnimator = ValueAnimator.ofInt(view.measuredHeight, newHeight); animation.addUpdateListener(object: ValueAnimator.AnimatorUpdateListener { override fun onAnimationUpdate(valueAnimator: ValueAnimator?) { val animatedValue = valueAnimator!!.animatedValue val layoutParams: ViewGroup.LayoutParams = view.layoutParams layoutParams.height = animatedValue as Int view.layoutParams = layoutParams } }) animation.duration = SLIDE_DURATION animation.start() delay() }

Android – ViewDataBinding.inflate不会膨胀嵌套的自定义视图

我真的难住Android的视图通胀问题。 我看了其他示例代码使用android.databinding,我似乎无法理解为什么这不起作用。 任何有Android的经验的人可以提供一些建议? 我创建了一个简单的自定义视图: class TestView(context: Context) : View(context) { private var testCallBack: TestCallback? = null fun setTestCallback(callback: TestCallback) { testCallBack = callback } interface TestCallback { fun test() } } 然后把它包含在一个简单的布局中: <?xml version="1.0" encoding="utf-8"?> <layout 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.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".application.home.HomeActivity"> <com.test.TestView android:id="@+id/testView" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.constraint.ConstraintLayout> </layout> 然后我膨胀我的片段onCreateView方法的布局,并使用视图绑定库,尝试设置我的自定义视图的回调: class TestFragment : TiFragmentBase<TiPresenterHolder, TiView>(), […]