【问题标题】:Java/Android - Make a button visible after a radio button has been clickedJava/Android - 单击单选按钮后使按钮可见
【发布时间】:2017-09-08 04:29:32
【问题描述】:

我正在创建一个基本的测验应用程序,向用户显示一个又一个问题。

为了回答下一个问题,用户必须点击一个按钮。

现在,我只希望在用户选择一个可能的答案(单选按钮)后,这个按钮可以让用户看到下一个问题。 选择了 RadioGroup 中的哪个 RadioButton无关,重要的是他点击了答案! 我找到了一种方法,但我不确定这是不是最好的方法:

> public void startQuiz(View view) {
>     setContentView(R.layout.question_1);
>     RadioGroup rg1 = (RadioGroup) findViewById(R.id.r_group_1);
>     rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
>         @Override
>         public void onCheckedChanged(RadioGroup group, int checkedId) {
>             switch (checkedId) {
>                 case R.id.radio_button_a_q1:
>                     View nb1 = findViewById(R.id.nb1);
>                     nb1.setVisibility(View.VISIBLE);
>                     break;
>                 case R.id.radio_button_b_q1:
>                     nb1 = findViewById(R.id.nb1);
>                     nb1.setVisibility(View.VISIBLE);
>                     break;
>                 case R.id.radio_button_c_q1:
>                     nb1 = findViewById(R.id.nb1);
>                     nb1.setVisibility(View.VISIBLE);
>                     break;
>             }
>         }
>     }
>     ); }

提前非常感谢您! :-)

【问题讨论】:

  • 你试过什么?向我们展示一些您无法开始工作的代码。通常这是在取消隐藏隐藏按钮(alpha 或可见性)的单选按钮上设置侦听器

标签: java android xml button android-radiobutton


【解决方案1】:

试试这个

 radiobutton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return false;
        }
    });

【讨论】:

    【解决方案2】:
    if(radioButton.isChecked()){
        button.setVisibility(View.VISIBLE);
    else{
        button.setVisibility(View.GONE);
    }
    

    【讨论】:

    • 我确实想要这种行为,但我想将它应用到整个 RadioGroup 并询问是否可以不将代码应用到组的每个单选按钮。
    • if(radioButton1.isChecked() || radioButton2.isChecked() || radioButton3.isChecked()){ button.setVisibility(View.VISIBLE); else{ button.setVisibility(View.GONE);或者你可能想看看这个 stackoverflow.com/questions/19108686/…>
    【解决方案3】:

    如果我正确理解了这个问题,你可能想要这样的东西

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.Toggle;
    import javafx.scene.control.ToggleGroup;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Test extends Application{
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            GridPane root = new GridPane();
            root.setAlignment(Pos.CENTER);
            root.setHgap(20);
            VBox container = new VBox();
            container.setAlignment(Pos.CENTER);
    
            final ToggleGroup group = new ToggleGroup();
            RadioButton rb1 = new RadioButton("First Choice");
            rb1.setToggleGroup(group);
            RadioButton rb2 = new RadioButton("Second Choice");
            rb2.setToggleGroup(group);
            RadioButton rb3 = new RadioButton("Third Choice");
            rb3.setToggleGroup(group);
    
            container.getChildren().addAll(rb1,rb2,rb3);
    
            Button btn = new Button("I should appear");
            btn.setVisible(false);
    
            root.add(container, 0, 0);
            root.add(btn, 1, 0);
    
            group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
                public void changed(ObservableValue<? extends Toggle> ov,
                     Toggle old_toggle, Toggle new_toggle) {
                        if (group.getSelectedToggle() != null) {
                            btn.setVisible(true);
                        }                
                    }
            });
    
            Scene s = new Scene(root,400,200 );
            primaryStage.setScene(s);
            primaryStage.setResizable(false);
            primaryStage.setTitle("Example");
            primaryStage.show();    
    
    
        }
    
        public static void main(String[] args){
            launch();
        }
    
    }
    

    编辑:这个使用 JavaFX ,我不了解 android。

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多