如题,安卓初学者,没有什么复杂功能,只是拖上去又删掉了
<?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">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.095" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="263dp"
android:layout_height="131dp"
android:layout_marginStart="72dp"
android:layout_marginTop="104dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">
<RadioButton
android:id="@+id/rbFirst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hh " />
<RadioButton
android:id="@+id/rbSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hoho" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rgText" />
</RadioGroup>
<Button
android:id="@+id/btnOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
android:layout_marginTop="72dp"
android:text="@string/txtBtnOne"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="82dp"
android:layout_height="31dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="12dp"
android:text="@string/txtcb2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="80dp"
android:layout_height="33dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="16dp"
android:text="@string/txtcb1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup"
app:layout_constraintVertical_bias="0.2" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="81dp"
android:layout_height="30dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="51dp"
android:text="@string/txtcb3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnOne.setOnLongClickListener(View.OnLongClickListener {
(it as Button).text = this.getString(R.string.txtBtnChanged)
true
})
btnOne.setOnClickListener(View.OnClickListener {
click2Next()
})
radioGroup.setOnCheckedChangeListener{
group, checkedId ->
textView2.text = when(checkedId){
R.id.rbFirst -> "第一个"
R.id.rbSecond -> "第二个"
else ->""
}
}
checkBox.setOnCheckedChangeListener(myOnCheckChangedListener)
checkBox2.setOnCheckedChangeListener(myOnCheckChangedListener)
checkBox3.setOnCheckedChangeListener(myOnCheckChangedListener)
}
fun click2Next(){
intent = Intent(this,NextActivity::class.java).apply {
putExtra("Label",btnOne.text)
}
this.startActivity(intent)
}
private val myOnCheckChangedListener = CompoundButton.OnCheckedChangeListener{
buttonView, isChecked ->when(buttonView.id){
R.id.checkBox -> {
if(isChecked){
checkBox.text="Checked"
} else {
checkBox.text = this.getText(R.string.txtcb1)
}
}
R.id.checkBox2 -> {
val temp = buttonView.text
if(isChecked){
checkBox2.text = "Checked"
} else {
checkBox2.text = this.getText(R.string.txtcb2)
}
}
R.id.checkBox3 -> {
val temp = buttonView.text
if(isChecked){
checkBox3.text = "Checked"
} else {
checkBox3.text = this.getText(R.string.txtcb3)
}
}
}
}