简体   繁体   English

Android Spinner - 如何删除单选按钮?

[英]Android Spinner - How to remove radio buttons?

In Android 1.6, upon tapping a spinner (drop-down menu), radio buttons appear next to the spinner options. 在Android 1.6中,点击微调器(下拉菜单)后,微调器选项旁边会出现单选按钮。 How do I remove those radio buttons so that just the option text remains? 如何删除这些单选按钮,以便只保留选项文本?

Just to remove the radio buttons, you don't need your own adapter class. 只是要删除单选按钮,您不需要自己的适配器类。

Create a dropdown_item.xml in layout 在布局中创建dropdown_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee" />

then make the following call in the code. 然后在代码中进行以下调用。

arrayAdapter.setDropDownViewResource(R.layout.dropdown_item);

The default spinner dropdown item is a CheckedTextView which has the radio button. 默认的微调器下拉项是具有单选按钮的CheckedTextView。 Here you replace it with a TextView. 在这里,您可以使用TextView替换它。

You can use the android layout 你可以使用android布局

android.R.layout.simple_spinner_item 

instead of 代替

android.R.layout.simple_spinner_dropdown_item

but I recommend @kimkunjj answer, it will give you the control of the layout. 但我建议@kimkunjj回答,它会给你控制布局。

If you want to get rid of radio buttons on the spinner list you have to provide your own layout for row. 如果你想摆脱微调器列表上的单选按钮,你必须为行提供自己的布局。
Take a look at the example below: 看看下面的例子:


package com.ramps;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class MySpinner extends Activity {
    //data that will be used as a spinner options
    private static String data[] = {"one", "two", "three"};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //main.xml file contains spinner
        setContentView(R.layout.main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        //create your own adapter
        MySpinnerAdapter adapter = new MySpinnerAdapter(this,R.layout.custom_spinner_row,R.id.text, data );
        //set your custom adapter 
        spinner.setAdapter( adapter );
    }


    private class MySpinnerAdapter extends ArrayAdapter{

        public MySpinnerAdapter(Context context, int resource,
                int textViewResourceId, String[] objects) {
            super(context, resource, textViewResourceId, objects);          
        }   

    }
}


The custom layout for spinner row is just a simple LinearLayout with one TextView element which id is "text" (android:id="@+id/text") 微调器行的自定义布局只是一个简单的LinearLayout,带有一个TextView元素,其id为“text”(android:id =“@ + id / text”)

This is just simple example. 这只是一个简单的例子。 If you need more fancy layout than just TextView you would probably have to override getView() method of MySpinnerAdapter. 如果你需要比TextView更多的花哨布局,你可能必须覆盖MySpinnerAdapter的getView()方法。

"android.R.layout.simple_spinner_item" does the job, “android.R.layout.simple_spinner_item”完成了这项工作,

programmatically that's: 以编程方式:

modeSpinner=new Spinner(layout.getContext());
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(layout.getContext(),     
    android.R.layout.simple_spinner_item, Arrays.asList(modes));
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

If you have included android support library version 18 or above into your project then you can replace the resource id simple_spinner_dropdown_item with support_simple_spinner_dropdown_item . 如果您已包括支持Android库版本18或以上到你的项目,那么你可以替换的资源ID simple_spinner_dropdown_itemsupport_simple_spinner_dropdown_item That will remove the radio button. 这将删除单选按钮。

使用simple_dropdown_item_1line

The "cleanest" way of doing this (just remove the check mark and not touch anythinbg else including text style, item size etc.) is to create custom adapter (like in the answers above): 这种“最干净”的方式(只需删除复选标记而不触及任何其他内容,包括文本样式,项目大小等)是创建自定义适配器(如上面的答案中所示):

public class SimpleSpinnerArrayAdapter extends ArrayAdapter<String> {

    public SimpleSpinnerArrayAdapter(Context context, String[] data) {
        super(context, android.R.layout.simple_spinner_item, data);
        this.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    public SimpleSpinnerArrayAdapter(Context context, List<String> data) {
        super(context, android.R.layout.simple_spinner_item, data);
        this.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    /**
     * Returns default dropdown view with removed checkbox
     */
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        if (view != null && view instanceof CheckedTextView) {
            ((CheckedTextView) view).setCheckMarkDrawable(null);
        }
        return view;
    }
}

Please notice the getDropDownView() method which returns the view for dropdown list item. 请注意getDropDownView()方法,该方法返回下拉列表项的视图。 You can use any custom view here but if you would like to stick to the default view you should probably use the above code. 您可以在此处使用任何自定义视图,但如果您想坚持默认视图,则应该使用上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM