我的主要活动有两个Double输入和两个广播组。我可以将Double输入从Main传递到第二个活动,但是我不知道如何传递单选按钮选择。
对于Double输入,这是我在主要活动中拥有的内容:
val intent = intent
var result: Double = 0.0
val a = intent.getDoubleExtra("score1", 0.0)
val b = intent.getDoubleExtra("score2", 0.0)
result = a + b
这是我在第二活动中得到的:
If (rbBlack.isChecked) {
//do xyz
}else {
do abc}
我如何将单选按钮传递给“第二活动”?
rbgroup1有两个选项:“黑白”和rbgroup2有四个选项:“秋季”,“春季”,“夏季”和“冬季”。目标是Second Activity中的if语句,看起来像这样:
If (rbFall.isChecked) {
//do xyz
}else if (rbSpring.isChecked){
do abc
}else if(rbSummer.isChecked){
do blah blah
}else blah
然后
A
答案 0 :(得分:0)
下面是代码,当您选择单选按钮时,它将文本放置在常规按钮上
首先使用XML代码
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/tvValOne"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="32dp"
android:paddingBottom="8dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="Value One"
android:textAlignment="textEnd"
android:textColor="@color/color_Black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvValTwo"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:paddingBottom="8dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="Value Two"
android:textAlignment="viewEnd"
android:textColor="@color/color_Black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/tvValOne"
app:layout_constraintTop_toBottomOf="@+id/tvValOne" />
<TextView
android:id="@+id/tvANS"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="32dp"
android:paddingBottom="8dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="Answer"
android:textAlignment="textEnd"
android:textColor="@color/color_deepBlue"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/tvValTwo"
app:layout_constraintTop_toBottomOf="@+id/tvValTwo" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3203125" />
<EditText
android:id="@+id/etValOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number|numberDecimal"
android:textColor="@color/color_Black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/etValTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number|numberDecimal"
android:textColor="@color/color_Black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/etValOne" />
<EditText
android:id="@+id/etANS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number|numberDecimal"
android:textColor="@color/color_Black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/etValTwo" />
<Button
android:id="@+id/btnLAMmultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="120dp"
android:onClick="onLAMmultiply"
android:padding="16dp"
android:text="Select Function"
android:textColor="@color/color_Purple"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/etANS" />
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginStart="32dp"
android:onClick="onBACK"
android:padding="16dp"
android:text="BACK"
android:textColor="@color/color_Purple"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tvError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="52dp"
android:layout_marginStart="32dp"
android:text="TextView"
android:textColor="@color/color_Red"
android:textSize="30sp"
android:textStyle="bold"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline2" />
<RadioGroup
android:id="@+id/rgRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/btnLAMmultiply" >
<RadioButton
android:id="@+id/rbAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value Addition"
android:textColor="@color/color_Purple"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/rbSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value Subtraction"
android:textColor="@color/color_deepBlue"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/rbMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value Multiplication"
android:textColor="@color/color_Red"
android:textSize="30sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/rbDivision"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value Division"
android:textColor="@color/color_Yellow"
android:textSize="30sp"
android:textStyle="bold" />
</RadioGroup>
现在参加活动
class PageThreeActivity : AppCompatActivity() {
var MathFunction:String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_page_three)
rgRadioGroup.setOnCheckedChangeListener( { group, checkedId ->
if (checkedId != -1) {
MathFunction = (findViewById<View>(checkedId) as RadioButton).getText().toString()
btnLAMmultiply.setText(MathFunction)
} else {
MathFunction = ""
btnLAMmultiply.setText("Math Function")
}
})
}// end onCreate
fun doFunction(view: View){
val id = rgRadioGroup.checkedRadioButtonId
when (id) {
R.id.rbAdd -> add(view)
R.id.rbSubtract -> subtract(view)
R.id.rbMultiply -> multiply(view)
R.id.rbDivision ->division(view)
else -> onLAMmultiply(view)
}
}
fun add(view: View){
val X = etValOne.text.toString()
val Y = etValTwo.text.toString()
val multB = {X:Double,Y:Double -> X.plus(Y)}
val df = DecimalFormat("0.00")
//val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
df.format(multB(X.toDouble(),Y.toDouble()))
etANS.setText(df.format(multB(X.toDouble(),Y.toDouble())).toString())
//etANS.setText(multB(X.toDouble(),Y.toDouble()).toString())
}
fun subtract(view: View){
val X = etValOne.text.toString()
val Y = etValTwo.text.toString()
val multB = {X:Double,Y:Double -> X.minus(Y)}
val df = DecimalFormat("0.00")
//val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
df.format(multB(X.toDouble(),Y.toDouble()))
etANS.setText(df.format(multB(X.toDouble(),Y.toDouble())).toString())
//etANS.setText(multB(X.toDouble(),Y.toDouble()).toString())
}
fun multiply(view: View){
val X = etValOne.text.toString()
val Y = etValTwo.text.toString()
val multB = {X:Double,Y:Double -> X.times(Y)}
val df = DecimalFormat("0.00")// Show Two Decimal places with trailing ZERO
//val df = DecimalFormat("#.##")// Show Two Decimal places no trailing Zero
df.roundingMode = RoundingMode.CEILING
df.format(multB(X.toDouble(),Y.toDouble()))
etANS.setText(df.format(multB(X.toDouble(),Y.toDouble())).toString())
//etANS.setText(multB(X.toDouble(),Y.toDouble()).toString())
}
fun division(view: View){
val X = etValOne.text.toString()
val Y = etValTwo.text.toString()
val multB = {X:Double,Y:Double -> X.div(Y)}
val df = DecimalFormat("0.00")
//val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
df.format(multB(X.toDouble(),Y.toDouble()))
etANS.setText(df.format(multB(X.toDouble(),Y.toDouble())).toString())
//etANS.setText(multB(X.toDouble(),Y.toDouble()).toString())
}
fun onLAMmultiply(view: View ){
if(etValOne.text.length == 0){
error("Enter First Value")
//toast("Enter First Value")
etValOne.requestFocus()
return@onLAMmultiply
}
if(etValTwo.text.length == 0){
error("Enter Second Value")
//toast("Enter Second Value")
etValTwo.requestFocus()
return@onLAMmultiply
}
if(rgRadioGroup.checkedRadioButtonId == -1){
error("SELECT A FUNCTION")
return@onLAMmultiply
}
doFunction(view)
}
fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
fun error(msg:String){
object : CountDownTimer(4000, 1000) {
override fun onTick(millisUntilFinished: Long) {
tvError.visibility = View.VISIBLE
tvError.setText(msg)
}
override fun onFinish() {
tvError.visibility = View.INVISIBLE
tvError.setText("")
}
}.start()
}
fun onBACK(view: View){
val intent = Intent(this@PageThreeActivity,MainActivity::class.java)
startActivity(intent)
}
//结束课堂 }
因为我们在XML`android:id =“ @ + id / btnLAMmultiply”中共享了如此多的代码,所以
`
这在活动中
rgRadioGroup.setOnCheckedChangeListener( { group, checkedId ->
if (checkedId != -1) {
MathFunction = (findViewById<View>(checkedId) as RadioButton).getText().toString()
btnLAMmultiply.setText(MathFunction)
} else {
MathFunction = ""
btnLAMmultiply.setText("Math Function")
}
})
}// end onCreate
欢迎来到Stackoverflow