如果单选按钮被选中,在Java中显示一个文本框。

huangapple go评论99阅读模式
英文:

If radio button is checked, display a text box in Java

问题

抱歉,您的代码中存在一些问题,需要进行修正。以下是修正后的代码:

public class MainActivity4 extends AppCompatActivity {

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

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

        // 设置点击事件监听器
        View.OnClickListener radioClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = ((RadioButton) view).isChecked();

                switch (view.getId()) {
                    case R.id.radio_mountains:
                    case R.id.radio_sights_and_landmarks:
                    case R.id.radio_water_sports:
                    case R.id.radio_hiking:
                    case R.id.radio_road_trips:
                    case R.id.radio_beaches:
                    case R.id.radio_city_travel:
                    case R.id.radio_wildlife_viewing:
                    case R.id.radio_glaciers:
                    case R.id.radio_deserts:
                    case R.id.radio_coasts:
                        if (checked) {
                            findViewById(R.id.textView4).setText("Button1 has been chosen");
                        }
                        break;
                    default:
                        // 其他情况
                        break;
                }
            }
        };

        // 为每个 RadioButton 设置点击事件监听器
        rb1.setOnClickListener(radioClickListener);
        rb2.setOnClickListener(radioClickListener);
        rb3.setOnClickListener(radioClickListener);
        rb4.setOnClickListener(radioClickListener);
        rb5.setOnClickListener(radioClickListener);
        rb6.setOnClickListener(radioClickListener);
        rb7.setOnClickListener(radioClickListener);
        rb8.setOnClickListener(radioClickListener);
        rb9.setOnClickListener(radioClickListener);
        rb10.setOnClickListener(radioClickListener);
        rb11.setOnClickListener(radioClickListener);
        rb12.setOnClickListener(radioClickListener);
        rb13.setOnClickListener(radioClickListener);
    }
}

这段代码修复了您提到的问题,它为每个 RadioButton 设置了点击事件监听器,并根据选择的选项更新了相应的 TextView。希望这可以帮助您解决问题。

英文:

I am trying to create a quiz like app. Basically, the user selects their options and it displays the results at the end (in the form of recommendations on where to travel to next for example). So if two specific radio buttons are checked, then how can I display a text box in which I can provide recommendations in the form of text? I would really appreciate it if someone could guide me in the right direction. Thank you.

Again, I have found many answers in JS or Jquery but none in Java which is why I had to ask. I am using Android Studio.

EDIT:(this is my code, but I am unsure of how to fix these errors (the errors are view, getId, and 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;
}
}
{
}
}}`

答案1

得分: 0

I'm not sure of your experience level with android studio, so I'm going to assume you know how to use OnClickListeners. If not, check out this tutorial. Tutorial Link

Rather than creating a TextView on a click, try having an empty TextView, and then simply change the text inside. Try using this method for your 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;
    }
}

You'll have to change the "R.id.something" to the appropriate ids. I trust you'll be able to use the method, or use the useful parts. Hope I helped!

英文:

I'm not sure of your experience level with android studio, so I'm going to assume you know how to use OnClickListeners. If not, check out this tutorial. https://www.youtube.com/watch?v=01gOTzT2v-0

Rather than creating a TextView on a click, try having an empty TextView, and then simply change the text inside. Try using this method for your 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;
}
}

You'll have to change the "R.id.something" to the appropriate ids. I trust you'll be able to use the method, or use the useful parts. Hope I helped!

答案2

得分: -1

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;
    }
}
英文:
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;
}
}

huangapple
  • 本文由 发表于 2020年8月21日 03:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63511753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定