не получается изменять элементы в активити программно
разметка:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/editlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearlayoutnamebutton"
android:layout_width="match_parent"
android:layout_height="60dp">
<EditText
android:id="@+id/nameplaintext"
android:layout_width="362dp"
android:layout_height="match_parent"
android:ems="10"
android:hint="Заголовок"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:background="@android:color/transparent"
android:text="Button"
app:icon="@drawable/free_icon_microphone_3364184"
app:iconGravity="top"
app:iconSize="43dp"
app:iconTint="#000000"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addchecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="48dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_input_add"
tools:ignore="SpeakableTextPresentCheck,ImageContrastCheck" />
<LinearLayout
android:id="@+id/linearlistlayout"
android:layout_width="411dp"
android:layout_height="668dp"
android:orientation="vertical"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="115dp"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="@+id/linearlayoutlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="CheckBox" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<MultiAutoCompleteTextView
android:id="@+id/noteplaintext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:filterTouchesWhenObscured="false"
android:fitsSystemWindows="false"
android:gravity="start"
android:hint="Заметка"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
разметка выглядит так:
из кода я пытаюсь добавить кликлистенер элементам, изменить бакгроунд и отобразить выборочно какие-то, но ничего из этого не работает способы программного воздействия на разметку правильны, ибо переписал этот экран на фрагмент и там все работало нормально , но мне нужен именно активити, и дело я так понимабю именнол в активити, ибо в других активити нет таких проблем
програмное изменение:
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
var list = false
binding.linearlayoutnamebutton.button.setOnClickListener {
if (list){
binding.linearlayoutnamebutton.nameplaintext.background = resources.getDrawable(R.drawable.edittext)
}
else {
binding.linearlayoutnamebutton.nameplaintext.background = resources.getDrawable(R.drawable.edittext)
binding.framelayout.noteplaintext.background = resources.getDrawable(R.drawable.edittext)
}
binding.linearlayoutnamebutton.nameplaintext.setOnClickListener {
var intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
startActivityForResult(intent, 0)
binding.linearlayoutnamebutton.nameplaintext.background = null
binding.framelayout.noteplaintext.background = null
}
binding.framelayout.noteplaintext.setOnClickListener {
var intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
startActivityForResult(intent, 1)
binding.linearlayoutnamebutton.nameplaintext.background = null
binding.framelayout.noteplaintext.background = null
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == AppCompatActivity.RESULT_OK && data != null) {
if (requestCode == 0) {
val voice: ArrayList<String>? =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) as ArrayList<String>
binding.nameplaintext.setText(
binding.nameplaintext.text.toString() + " " + voice?.get(0)
)
}
if (requestCode == 1) {
val voice: ArrayList<String>? =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) as ArrayList<String>
binding.noteplaintext.setText(
binding.noteplaintext.text.toString() + " " + voice?.get(0)
)
}
}
}
}
Источник: Stack Overflow на русском