2016-05-04 83 views
1

我正在一個android項目上工作,我想用單選按鈕顯示微調數據,請建議我如何做到這一點。這是我的代碼如何在android中單擊微調器時顯示單選按鈕?

Spinner spinner = (Spinner) findViewById(R.id.sMonth); 
spinner.setOnItemSelectedListener(this); 
ArrayList categories=new ArrayList(); 
categories.add("Month"); 
categories.add("Jan"); 
categories.add("Feb"); 
categories.add("Mar"); 
categories.add("Apr"); 
categories.add("May"); 
categories.add("Jun"); 
categories.add("Jul"); 
categories.add("Aug"); 
categories.add("Sep"); 
categories.add("Oct"); 
categories.add("Nov"); 
categories.add("Dec"); 
ArrayAdapter dataAdapter=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,categories); 
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinner.setAdapter(dataAdapter); 
+0

它,而不是默認的一個 –

回答

0

String.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="app_name">AndroidSpinnerExample</string> 
    <string name="spinner_title">Select Category</string> 
</resources> 

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:padding="10dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <!-- Text Label --> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:text="Category:" 
     android:layout_marginBottom="5dp" 
    /> 

    <!-- Spinner Element --> 
    <Spinner 
     android:id="@+id/spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:prompt="@string/spinner_title" 
    /> 
</LinearLayout> 

JAVA代碼

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Spinner element 
     Spinner spinner = (Spinner) findViewById(R.id.spinner); 

     // Spinner click listener 
     spinner.setOnItemSelectedListener(this); 

     // Spinner Drop down elements 
     List&lt;String&gt; categories = new ArrayList&lt;String&gt;(); 
     categories.add("Automobile"); 
     categories.add("Business Services"); 
     categories.add("Computers"); 
     categories.add("Education"); 
     categories.add("Personal"); 
     categories.add("Travel"); 

     // Creating adapter for spinner 
     ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, categories); 

     // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     // attaching data adapter to spinner 
     spinner.setAdapter(dataAdapter); 
    } 

    @Override 
    public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { 
     // On selecting a spinner item 
     String item = parent.getItemAtPosition(position).toString(); 

     // Showing selected spinner item 
     Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); 

    } 

    public void onNothingSelected(AdapterView&lt;?&gt; arg0) { 
     // TODO Auto-generated method stub 

    } 

} 

輸出

enter image description here

+0

謝謝你的幫助,但我想在微調列表單選按鈕創建自定義佈局?可能嗎? –

+0

你用我的答案嘗試嗎?你得到了什麼輸出? –

+0

@NomanMalik檢查我編輯的答案 –

4

,你只需要在轉適配器上下拉添加的Android佈局select_dialog_singlechoice,如:......

adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice); 
+0

這是一個完美的答案。在較舊版本的Android中,單選按鈕是'android.R.layout.simple_spinner_dropdown_item'的一部分,但它不再是。 'android.R.layout.select_dialog_singlechoice'就是答案。對於那些使用自定義佈局的人,只需添加'android:checkMark =「?android:attr/listChoiceIndicatorSingle」'和'android:gravity =「center_vertical」'(使單選按鈕與文本對齊)。 –

+0

@this是我API版本的解決方案 –

0
import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.Toast; 

public class MyAndroidAppActivity extends Activity { 

    private Spinner spinner1, spinner2; 
    private Button btnSubmit; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

addItemsOnSpinner2(); 
addListenerOnButton(); 
addListenerOnSpinnerItemSelection(); 
    } 

    // add items into spinner dynamically 
    public void addItemsOnSpinner2() { 

    spinner2 = (Spinner) findViewById(R.id.spinner2); 
    List<String> list = new ArrayList<String>(); 
    list.add("list 1"); 
    list.add("list 2"); 
    list.add("list 3"); 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, list); 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner2.setAdapter(dataAdapter); 
    } 

    public void addListenerOnSpinnerItemSelection() { 
    spinner1 = (Spinner) findViewById(R.id.spinner1); 
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener()); 
    } 

    // get the selected dropdown list value 
    public void addListenerOnButton() { 

    spinner1 = (Spinner) findViewById(R.id.spinner1); 
    spinner2 = (Spinner) findViewById(R.id.spinner2); 
    btnSubmit = (Button) findViewById(R.id.btnSubmit); 

    btnSubmit.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 


     } 

    }); 
    } 
} 

//CustomOnItemSelectedListener.java

import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Toast; 

public class CustomOnItemSelectedListener implements OnItemSelectedListener { 

     public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) { 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 
    } 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/country_arrays" 
     android:prompt="@string/country_prompt" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btnSubmit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Submit" /> 

</LinearLayout> 

//string.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Yourapp</string> 
    <string name="country_prompt">Choose a country</string> 

    <string-array name="country_arrays"> 
     <item>Malaysia</item> 
     <item>United States</item> 
     <item>Indonesia</item> 

    </string-array> 

</resources> 
相關問題