各位程序员好!
我尝试从我的xml文件示例中调用元素:我的activity.xml中的一个“按钮”,但是当我尝试在代码中调用它时,MainActivity.kt无法解析我的ID 我试图清理并重新构建项目,但仍然有问题也使高速缓存重新启动无效
像这样,我想从acitivty_main.xml中调用按钮
button.setOnClickListener { }
MainActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.mostafa.stringlength.R.id.editText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
按钮。(我在这里尝试调用该按钮),但什么都没显示//
}
}
xml活动:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="138dp"
android:layout_height="58dp"
android:layout_marginStart="136dp"
android:layout_marginEnd="137dp"
android:layout_marginBottom="212dp"
android:text="أظهار النتيجة"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="411dp"
android:layout_height="360dp"
android:layout_marginStart="43dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="ادخل النص هنا"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="417dp"
android:layout_height="195dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
android:text="النتائج:"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:1)
现在,插件部分如下:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
答案 1 :(得分:0)
在顶部添加以下导入语句
const FancyButton = React.forwardRef(
function Button(props, ref) {
return (
<button ref={ref} className="FancyButton">
{props.children}
</button>
)
}
);
答案 2 :(得分:0)
确保gradle文件顶部有这2行
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
放入
import kotlinx.android.synthetic.main.activity_main.*
在您的活动文件中
答案 3 :(得分:0)
有一种更好的方法叫做视图绑定。它使事情变得容易得多。当您使用它时,您将不需要 'kotlin-android-extensions'
。
https://developer.android.com/topic/libraries/view-binding#java
答案 4 :(得分:0)
Kotlin Android 扩展已被弃用。您可以使用 Jetpack 提供的数据绑定或视图绑定。
您只需要在 Module build.gradle 文件中包含这些行:
android {
...
buildFeatures {
viewBinding true
}
}
将生成基于您的 Activity 名称的绑定对象,例如 MainActivity 将生成 ActivityMainBinding
private lateinit var binding: ActivityMainBinding
您现在可以使用此绑定变量来扩充视图并引用布局中的视图:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.result.text = "My “Result!"
}
}
答案 5 :(得分:0)
示例解决方案
val btn_start = findViewById<Button>(R.id.btnStart)
btn_start.setOnClickListener{
//Your code
}