我正在尝试在选择Spinner中的某个Item时显示EditText。到目前为止,我已经创建了微调器和EditText,但我真的不知道我的下一步是什么。 我不想在EditText中显示选择,我只想显示EditText字段。
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edit_text_box);
editText.getText().toString();
}
public void addItemsOnSpinner()
{
spinner1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.spinner_item, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter (adapter);
}
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
editText.setText(spinner1.getSelectedItem());
}
这也是我在字符串数组中所拥有的内容+希望仅在选择item2时显示edittext框
<string-array name="spinner_item">
<item>Item 1</item>
<item value="Item2">Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
答案 0 :(得分:2)
您应该将editText的可见性设置为不可见或以XML格式显示,取决于更适合您的需求。那么您可以使用以下内容来显示它:
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
yourEditText.setVisibility(View.VISIBLE);
}
答案 1 :(得分:1)
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int x = spinner1.getSelectedItemPosition();
valSel = items[x];
}
String spinner1val = spin1.getText().toString(); //extract value of text from spinner
EditText text = (EditText) findViewById(R.id.EditText1); //get id of EditText box
text.setText(spinner1val);
或者,如果要检查在Spinner中选择的项的值,则“valsel”包含该值。只是祝酒词。
答案 2 :(得分:0)
您必须在edittext onItemSelected
上设置文字。首先根据 MKJParekh 建议进行更改。
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
yourEditText.setText(spinner1.getSelectedItem())
}
答案 3 :(得分:0)
从微调器中获取所选值
String anyvariable=String.valueOf(spin.getSelectedItem());
现在,您可以在编辑文本
中显示此字符串值EditText text = (EditText) findViewById(R.id.your_text);
text.setText(anyvariable);
答案 4 :(得分:0)
@Override
public void onItemSelected(AdapterView<?> month, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
selectedMonth= month.getItemAtPosition(arg2).toString();
Log.d("Tag",""+selectedMonth);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//selected_item= arg0.getChildAt(1).toString();
}
});
selected_item contains the item ,so just set it to editView like
editView.setText(selected_item)
答案 5 :(得分:0)
EditText height1,weight1,height,weight; Spinner height_spinner, weight_spinner; String heightInputString,weightInputString;按键 calculatebmi;
@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_bmi);
// set your class members as they start out null.
// do this for all of them
height1 = (EditText) findViewById(R.id.idofheight1inxml);
height_spinner = (Spinner) findViewById(R.id.idofheightspinnerinxml);
....
// Show the Up button in the action bar.
setupActionBar();
setupSpinners(); }
void setupSpinners(){ height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ @覆盖 public void onItemSelected(AdapterView parent,View view,int position,long id){ //I.E。如果在高度微调器CM中被选中,我想隐藏第二个高度的edittext字段。 //我不确定这是不是“height1”或“height” if(position == 0){ height.setVisibility(View.GONE); } else { height.setVisibility(View.VISIBLE); } }
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// if you want to add similar logic for weight spinner, do that with this :
weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// put your code here for weight spinner
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}); }