Help! I need a Spinner, Radio Group with Radio buttons, EditText, and setText
Ok, I'm not great at explaining this as I'm new to Android so work with me. I have tried everything I can find and I dont know how to make everything work together. I have a Spinner to select an age group, a Radio group with Radio buttons to allow you to select Gender (male or female), I have an EditText that I will allow for numerical input and a Textview out to the side that is currently blank.
Depending on which age group is selected from the spinner and which one of the Radio buttons are selected - setText of the TextView to a certain number based on what number is entered in the EditText box.
Ive tried using OnItemSelectedListener for the spinner but havent been able to find how it knows what is selected from the array.I tried getItemSelected with an开发者_开发百科 if statement with the item from the array
for the radio button Ive tried using an if statement - if (radiobutton.isChecked()) but I didnt know if I needed an ifCheckedRadio before this and how to make it work after the spinner
and if all of this worked I had if (EditText.getText().equals("#")) TextView.setText("#");
This will be copied for each possible # entered
now would the text change like this without a button and OnClickListener or is there a way to do this without a button becuase there will more than one EditText that will be added at the end using a button but I wanted this displayed immediately after it is entered
Sorry if this isnt clear, please let me know if you need more information. If I've overlooked a previous question that uses spinners and radiobuttons please let me know and I'll keep looking but I've been stuck on this.
OK - re your last comment, this makes both selecting an item from the spinner and the radio buttons, terminating actions for input. Basically I've added onClickListeners to the buttons. You can't really add an Onclick to the edit text though, as a click normally brings up the soft keyboard.
package com.test.spinnerexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerExample extends Activity {
RadioButton rb1; RadioButton rb2; TextView tv; EditText et;
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rb1 = (RadioButton) findViewById(R.id.RadioButton01);
rb2 = (RadioButton) findViewById(R.id.RadioButton02);
tv = (TextView) findViewById(R.id.OutputText);
et = (EditText) findViewById(R.id.EditText01);
sp = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter1;
adapter1 = ArrayAdapter.createFromResource(
this, R.array.agegroups_array,
android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter1);
rb1.setChecked(true);
sp.setOnItemSelectedListener(new MyOnItemSelectedListener());
rb1.setOnClickListener(new ButtonListener());
rb2.setOnClickListener(new ButtonListener());
}
private class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
RadioButton checkedButton = rb1.isChecked() ? rb1 : rb2;
showResult(parent.getItemAtPosition(pos).toString(),
checkedButton.getText().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
private class ButtonListener implements OnClickListener{
@Override
public void onClick(View view) {
RadioButton rb = (RadioButton) view;
int selectedItem = sp.getSelectedItemPosition();
showResult(sp.getItemAtPosition(selectedItem).toString(),
rb.getText().toString());
}
}
private void showResult(String selctdSpnrText, String chkdBtnText){
String answer = selctdSpnrText + " " + chkdBtnText + " "
+ et.getText();
tv.setText(answer);
}
}
This should do it
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
TextView tv = (TextView) findViewById(R.id.TextView01);
RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup01);
EditText et = (EditText) findViewById(R.id.EditText01);
RadioButton rb1 = (RadioButton) findViewById(R.id.RadioButton01);
RadioButton rb2 = (RadioButton) findViewById(R.id.RadioButton02);
String gender = "";
if (rb1.isChecked())
gender = "male";
if (rb2.isChecked())
gender = "female";
String answer;
answer = parent.getItemAtPosition(pos).toString() + " "
+ gender + " " + et.getText();
tv.setText(answer);
}
- rough and ready but should give you the general idea (put the Radiobuttons inside the RadioGroup by the way)
// ------- spinner for gender
ArrayAdapter<String> spnAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spnAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSex.setAdapter(spnAdapter);
spnSex.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
myprofile.str_sex = items[position];
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
精彩评论