如果选中单选按钮,则显示java文本框

ogsagwnx  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(506)

我正在尝试创建一个类似测验的应用程序。基本上,用户选择他们的选项,并在最后显示结果(例如,以推荐下一个目的地的形式)。因此,如果选中了两个特定的单选按钮,那么如何显示一个文本框,以便以文本形式提供建议?如果有人能把我引向正确的方向,我会非常感激的。谢谢您。
同样,我在js或jquery中找到了很多答案,但在java中却没有找到答案,这就是为什么我要问这个问题。我用的是android工作室。
edit:(这是我的代码,但我不确定如何修复这些错误(错误是view、getid和settext)`public class mainactivity4 extends appcompatactivity{

private String view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    public void onClick(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        RadioButton rb1 = (RadioButton) findViewById(R.id.radio_mountains);
        RadioButton rb2 = (RadioButton) findViewById(R.id.radio_sights_and_landmarks);
        RadioButton rb3 = (RadioButton) findViewById(R.id.radio_water_bodies);
        RadioButton rb4 = (RadioButton) findViewById(R.id.radio_water_sports);
        RadioButton rb5 = (RadioButton) findViewById(R.id.radio_islands);
        RadioButton rb6 = (RadioButton) findViewById(R.id.radio_hiking);
        RadioButton rb7 = (RadioButton) findViewById(R.id.radio_road_trips);
        RadioButton rb8 = (RadioButton) findViewById(R.id.radio_beaches);
        RadioButton rb9 = (RadioButton) findViewById(R.id.radio_city_travel);
        RadioButton rb10 = (RadioButton) findViewById(R.id.radio_deserts);
        RadioButton rb11 = (RadioButton) findViewById(R.id.radio_wildlife_viewing);
        RadioButton rb12 = (RadioButton) findViewById(R.id.radio_glaciers);
        RadioButton rb13 = (RadioButton) findViewById(R.id.radio_coasts);

        switch(((RadioButton) view).findViewById()) {
            case R.id.radio_mountains:
            case R.id.radio_sights_and_landmarks:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_mountains:
            case R.id.radio_water_sports:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_mountains:
            case R.id.radio_hiking:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_mountains:
            case R.id.radio_road_trips:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_mountains:
            case R.id.radio_beaches:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_mountains:
            case R.id.radio_city_travel:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_mountains:
            case R.id.radio_wildlife_viewing:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_water_bodies:
            case R.id.radio_sights_and_landmarks:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_water_bodies:
            case R.id.radio_water_sports:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_water_bodies:
            case R.id.radio_hiking:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_water_bodies:
            case R.id.radio_road_trips:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_water_bodies:
            case R.id.radio_beaches:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_water_bodies:
            case R.id.radio_city_travel:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_sights_and_landmarks:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_water_sports:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_hiking:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_road_trips:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_beaches:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_city_travel:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_islands:
            case R.id.radio_wildlife_viewing:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_sights_and_landmarks:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_water_sports:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_hiking:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_road_trips:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_beaches:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_city_travel:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_glaciers:
            case R.id.radio_wildlife_viewing:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_sights_and_landmarks:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_water_sports:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_hiking:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_road_trips:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_beaches:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_city_travel:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_deserts:
            case R.id.radio_wildlife_viewing:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_sights_and_landmarks:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_water_sports:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_hiking:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_road_trips:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_beaches:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_city_travel:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;
            case R.id.radio_coasts:
            case R.id.radio_wildlife_viewing:
                if (checked)
                    findViewById(R.id.textView4).setText("Button1 has been chosen");
                break;

        }
    }

    {
        }

    }}`
jjjwad0x

jjjwad0x1#

我不确定你对androidstudio的经验水平,所以我假设你知道如何使用androidstudio OnClickListener s。如果没有,请查看本教程。https://www.youtube.com/watch?v=01gotzt2v-0
与其一次点击就创建一个textview,不如尝试使用一个空的textview,然后简单地更改里面的文本。尝试将此方法用于onclick:

public void onClick(View view) {
    boolean checked = ((RadioButton) view).isChecked();

    switch(view.getId()) {
        case R.id.button1:
            if (checked)
        findViewById(R.id.textView).setText("Button1 has been chosen");
            break;
        case R.id.button2:
            if (checked)
        findViewById(R.id.textView).setText("Button2 has been chosen");
            break;
    }
}

你得把“r.id.something”改成合适的id。我相信你能使用这个方法,或者使用有用的部分。希望我能帮忙!

hgc7kmma

hgc7kmma2#

TextView textView; 

public void onCreate(){
    textView = findViewById(R.id.paymentMethodTextView);
}
//Out of onCreate
@SuppressLint({"NonConstantResourceId", "SetTextI18n"})
public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch (view.getId()) {
        case R.id.cashPayment:
            if (checked)
                textView.setText("Button1 has been chosen");
            break;
        case R.id.cardPayment:
            if (checked)
                textView.setText("Button2 has been chosen");
            break;
    }
}

相关问题