2012-09-07 152 views
0

我試圖顯示一個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

<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> 
+2

變化對象名EDITTEXT或EDIT1或etName,'EditText'是不允許的。 – MKJParekh

回答

1

您應該設置EDITTEXT的能見度不可見或用XML取代了,更好地滿足您的需求。那麼你可以使用下面使其出現:

public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id) 
{ 
yourEditText.setVisibility(View.VISIBLE); 
} 
0

你必須設置上的EditText onItemSelected文本顯示的EditText框。首先按更改MKJParekh建議。

public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id) 
{ 
    yourEditText.setText(spinner1.getSelectedItem()) 
} 
+0

我遵循了MKJParekh的說法,並且嘗試了您的建議,但現在告訴我editText無法解析 – user1643333

+0

@ user1643333請顯示您的代碼 –

0

獲取從旋轉選定的值,

String anyvariable=String.valueOf(spin.getSelectedItem()); 

現在可以顯示在編輯文本這個字符串值,

EditText text = (EditText) findViewById(R.id.your_text); 
text.setText(anyvariable); 
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)  
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); 

另外,如果你想檢查項目的微調選擇的值時,「valsel」包含值。只是敬酒的中檔。

0
  1. EditText height1,weight1,height,weight; Spinner height_spinner, weight_spinner;字符串heightInputString,weightInputString;按鈕 calculatebmi;

    @Override保護無效的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(); } 
    

    空隙setupSpinners(){ height_spinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){ @Override 公共無效onItemSelected(適配器視圖父,瀏覽視圖,INT位置,長ID){// IE如果在高度微調CM被選中我想隱藏第二個高度edittext字段 //我不確定這是爲了「height1」還是「height」 if(position == 0){ height .setVisibility(View.GONE); } else { height.setVisibility(View.VISIBLE); } }的EditText的

    @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) { 
    
        } 
    }); } 
    
+0

您也可以嘗試此代碼 – chandan007

相關問題