Tag: kotlin android extensions

Kotlin删除所有非字母数字字符

我正在尝试从字符串中删除所有非字母数字字符。 我尝试使用正则表达式replace() ,如下所示: var answer = answerEditText.text.toString() Log.d("debug", answer) answer = answer.replace("[^A-Za-z0-9 ]", "").toLowerCase() Log.d("debug", answer) D /调试:测试。 ,更换 D /调试:测试。 ,更换 为什么标点符号仍然存在? 如何获得只有字母数字字符?

Android不确定ProgressBar不显示

我正在为我的Android应用程序添加一个旋转进度视觉,为了使它变得复杂,我目前只在我的onCreate中启动它,而不是结束它,因为它应该是非常简单的。 但是,遵循指定只包含进度条的正式Android文档 <ProgressBar android:id="@+id/weather_loading_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 对于不确定的加载条,它不起作用。 我试图增加indeterminate和visibility ,但没有运气。 <ProgressBar android:id="@+id/weather_loading_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminate="true" android:visibility="visible" /> 我使用Kotlin的Android扩展,所以在我的onCreate()我试过了 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //setVisibility here doesn't actually get shown as an option in the autocomplete, but still compiles. weather_loading_bar.setVisibility(View.VISIBLE) } 但是当我的应用程序启动时,没有进度条。 编辑:这是整个activity_main布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.MainActivity"> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/currentData" android:layout_width="match_parent" android:layout_height="wrap_content" […]

不能从最终的类继承

我刚刚创建了我的第一个android库。 在另一个应用程序中,我想从它扩展一个类。 但它显示了一个错误:“不能从最终的'library.com.kukaintro.activities.KukaIntro'”扩展。 正如你所看到的,没有一个超类是最终的。 如果我点击超级类KukaIntro(在应用程序不在图书馆)它说: 这是我第一次创造一个图书馆。 有人可以告诉我怎样才能解决这个问题?

Kotlin将函数扩展的函数类型化 – 可从Java调用?

我试图使用具体化类型作为扩展功能的函数,但我不认为这是可能的,因为我检查生成的字节码后,我发现方法签名是私人的,任何解决方法,使其公开? CommonExtensions.kt inline fun<reified T: Activity> Context.startActivity() { val intent = Intent(this, T:: class.java) startActivity(intent) } fun View.visible() { visibility = View.VISIBLE } Kotlin字节代码: private final static startActivity(Landroid/content/Context;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 … 客户代码: Kotlin文件 override fun showMessageEmptyOfferFeeds() { mOfferFeedsWarning.visible() // "visible()" extension func RESOLVED } Java文件 showProfileDetailsUi(){ startActivity<DetailActivity>() //"startActivity()" extension func NOT RESOLVED […]

我无法理解kotlin中string.kt的源代码实现

在kotlin源代码中,我无法理解如何实现String.kt的长度,如下所示: package kotlin public class String : Comparable<String>, CharSequence { companion object {} /** * Returns a string obtained by concatenating this string with the string representation of the given [other] object. */ public operator fun plus(other: Any?): String public override val length: Int public override fun get(index: Int): Char public override fun subSequence(startIndex: Int, […]

不幸的是,该应用程序已停止Android工作室,并没有错误

在Android监视器是这样的错误,如图片中,我不明白为什么是错误的XML? 代码没有问题,可以添加它的图片,使这个错误? class MainActivity : AppCompatActivity() { val ListOffood = ArrayList<Food>() var adpter:foodAdapter=null!! override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name)) ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name)) ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name)) ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name)) ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name)) adpter=foodAdapter(this,ListOffood) graidV.adapter=adpter } class foodAdapter: BaseAdapter { var ListOffood=ArrayList<Food>() var context: Context?=null constructor(context: Context, ListOffood:ArrayList<Food>):super(){ this.context=context this.ListOffood=ListOffood } override fun getView(p0: Int, p1: View?, p2: […]

Kotlin Android扩展缓存合成属性还是每次调用findViewById()?

如果我有一个简单的自定义视图: myitem.xml <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <FrameLayout/> 访问一个kotlinx syntentic属性: import kotlinx.android.synthetic.main.myitem.view.* view.toolbar.text = "Some text" 它在内部生成对findByViewID()的调用。 所以我的问题是: 结果缓存为自定义视图像活动或每次每次调用findByViewID被调用? 性能方面的原因,答案非常重要。

Kotlin Android扩展和菜单

有没有办法使用合成属性,而不是使用findItem方法来访问fragment_photo_gallery布局中定义的menu_item_search菜单项? override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) { super.onCreateOptionsMenu(menu, menuInflater) menuInflater.inflate(R.menu.fragment_photo_gallery, menu) //is there a way to access searchItem using synthetic properties? val searchItem = menu.findItem(R.id.menu_item_search) }

和…之间的不同 !! 和? 在Kotlin

我是Kotlin新手。 我想知道这两个之间的区别!! 和? 在下面的代码。 我有以下两个片段首先使用!! 为mCurrentDataset和另一个有? 对于相同的变量。 if(!mCurrentDataset!!.load(mDataSetString.get(mCurrentDataSelectionIndex), STORAGE_TYPE.STORAGE_APPRESOURCE)) { Log.d("MyActivity","Failed to load data.") return false } if(!mCurrentDataset?.load(mDataSetString.get(mCurrentDataSelectionIndex), STORAGE_TYPE.STORAGE_APPRESOURCE)!!) { Log.d("MyActivity","Failed to load data.") return false } 提前致谢。

点击一个文本视图来改变片段

我正在尝试使用Kotlin构建我的第一个Android应用程序,但是我陷入了一个非常简单的情况。 我正在使用Kotlin android扩展,我得到一个空指针异常。 <TextView android:id="@+id/tvGoToRegisterFrag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_sign_in" android:text="@string/haven_t_registered_yet" android:paddingTop="10dp" android:textColor="@color/colorAccent" android:layout_centerHorizontal="true"/> 用这个我用了 tvGoToRegisterFrag.setOnClickListener { goToRegister() } 我知道我可以使用 tvGoToRegisterFrag?.setOnClickListener { goToRegister() } 为了摆脱崩溃,但我仍然不知道为什么对象是空的,点击时不做任何事情。 所有这些都是在片段中完成的,如果这改变了任何东西。