【问题标题】:Check RadioGroup checked or not and get value to static int检查 RadioGroup 是否检查并获取静态 int 的值
【发布时间】:2011-12-26 05:13:58
【问题描述】:

  1. GOAL 1:单击按钮时,如果没有选中任何单选按钮,则会通过 Toast 警告用户;如果选中了一个单选按钮,它将把用户带到新的活动(或对你做 smt up)。

第一

public class hanh1_2 extends Activity{

public static int ButID;
@Override

二、设置按钮动作:

final Button ok2 = (Button) findViewById(R.id.ok2);
    ok2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Set int of ButID = checkedradiobuttonID
                            //If ButID = -1 --> there isn't bt checked
            int ButID = tieng.getCheckedRadioButtonId();
            if (ButID == -1){
                Toast.makeText(hanh1_2.this, "Check a butt pls", Toast.LENGTH_SHORT).show();
            }
            else {
            Intent intent2 = new Intent(hanh1_2.this,hanh1_3.class);
            startActivity(intent2);
            }
        }
    });

对高级毫无意义,但可能对像我这样的新手有帮助:)

【问题讨论】:

    标签: android static int radio-group


    【解决方案1】:
    1. 查看 Android 开发网站上的 Form stuff tutorial。您可以为所有 RadioButtons 提供 OnClickListener 并跟踪选定的一个(如果有)。

      private OnClickListener radio_listener = new OnClickListener() {
          public void onClick(View v) {
              // Perform action on clicks
              RadioButton rb = (RadioButton) v;
              Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
          }
      };
      

      或者,您可以使用 RadioGroup 的 getCheckedRadioButtonId() 方法。

    2. 如其他答案之一所示:将 int 值作为附加值传递给您用于启动第二个 Activity 的 Intent:

      // In first activity
      Intent i = new Intent(FirstActivity.this, SecondActivity.class);
      i.putInt("selected_index", selectedIndex);
      startActivity(i);
      
      // In second activity
      int selectedIndex = getIntent().getExtras().getInt("selected_index");
      

    【讨论】:

    • 谢谢,第 2 部分对我很有帮助。但是第 1 部分,我的目标是:当用户单击 ok2 时,如果有单选按钮检查,我会将他带到下一个活动,否则,我将 toast.makeText 警告他检查一个。
    • 我意识到这一点,但是根据我提供的信息,您应该不会有任何问题来实现那一小段逻辑。事实上,你已经拥有了 99% 的数据。只需将 index1 初始化为 1: index1 = -1; 就可以了。
    【解决方案2】:

    将您的所有RadioButtonRadioGroup 带到课堂水平。 在onCreate()中初始化它们

    现在在 onClick() 中获取已检查 RadioButton 的 id 并像这样比较:

    public void onClick(View v) {    
    
            int checked =   tieng.getCheckedRadioButtonId(); // tieng is your RadioGroup
    
            switch(checked)
            {
            case R.id.tieng1:
            Toast.makeText(hanh1_2.this, "First is selected", Toast.LENGTH_SHORT).show();
            break;
            case R.id.tieng1:
            Toast.makeText(hanh1_2.this, "Second is selected", Toast.LENGTH_SHORT).show();
            break;
            case R.id.tieng1:
            Toast.makeText(hanh1_2.this, "Third is selected", Toast.LENGTH_SHORT).show();
            break;
            default:
            Toast.makeText(hanh1_2.this, "pleas check any button", Toast.LENGTH_SHORT).show();
            break;
            }
    
    }
    

    【讨论】:

      【解决方案3】:

      将额外内容与意图一起添加:

      else {
              Intent intent2 = new Intent(hanh1_2.this,hanh1_3.class);
              intent2.putInt(Index1, index1);
              startActivity(intent2);
              }
      

      现在在第二个活动 onCreate() 中阅读以下内容:

      {
      int Index1 = getIntent().getExtras().getInt("Index1");
      
      //do stuff here
      

      }

      【讨论】:

      • 抱歉,下面 MH 的回答解决了我的两个问题,所以我接受了。当我有足够的声望点时,我会投票给你的答案!谢谢!
      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 2023-03-07
      • 2012-12-20
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多