[英]How to assign an integer value to a radiobutton?
目前,我在活動中有四個單選按鈕,每個按鈕代表一個值(例如10 $,50 $,100 $和200 $)。 現在,我想將這些數字的整數值分配給每個單選按鈕。
問題是當我嘗試實現多個解決方案(例如執行“ Case函數”或“ If-Else”循環)時,遇到各種錯誤,例如“ ==”不接受整數或分配給每個單選按鈕的變量從未使用過。
那么,如何為這些單選按鈕正確分配整數值呢?
“單選按鈕所在活動的Java代碼。”
package com.example.msi.fyp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import static com.example.msi.fyp.R.id.bProceed;
public class Process extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_process);
Button bProceed = (Button) findViewById(bProceed);
final RadioButton btn10 = (RadioButton) findViewById(R.id.btn10);
final RadioButton btn50 = (RadioButton) findViewById(R.id.btn50);
final RadioButton btn100 = (RadioButton) findViewById(R.id.btn100);
final RadioButton btn200 = (RadioButton) findViewById(R.id.btn200);
EditText credNum = (EditText) findViewById(R.id.credNum);
EditText cvv = (EditText) findViewById(R.id.cvv);
Radiogroup amnts = (Radiogroup) findViewById(R.id.amnts);
bProceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Process.this, done_donation.class));
}
});
}
}
(代碼看起來很短的原因是因為我刪除了上面提到的那些“單選按鈕值分配”代碼,因為它們給出了錯誤。
您可以使用setTag()
設置與此視圖關聯的標簽。 標簽可用於在其層次結構中標記視圖,並且在層次結構中不必唯一。 標簽還可以用於在視圖中存儲數據,而無需訴諸其他數據結構。
樣本代碼
radioButton.setTag(150);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.