Android的微调 - 如何的默认列表选择为无 [英] Android Spinner - How to default list selection to none

查看:238
本文介绍了Android的微调 - 如何的默认列表选择为无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android的形式,需要根据一定的选择进行自我更新。形式正在由2纱厂(A和B)。微调乙不创建,直到旋转器的选择的。后作出选择B将被显示在视图和它的内容基于A的选择动态填补。这里是我的code:

 公共类MyForm的扩展活动
{
    私人最终诠释SEL_ACTIVATE = 0;
    私人最终诠释SEL_DEACTIVATE = 1;    私有静态最后的String [] =的ActionList {激活,停用};    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        表=(TableLayout)findViewById(R.id.table);        showListA(表);
    }    公共无效showListA(视图V)
    {
        rowAction =新的TableRow(本);        微调微调=新的微调(本);
        spinner.setPrompt(选择...);
        spinner.setOnItemSelectedListener(
            新OnItemSelectedListener()
            {
                公共无效onItemSelected(适配器视图<>母公司,视图V,INT位置,长的ID)
                {
                    开关(位置)
                    {
                    案例SEL_ACTIVATE:
                    案例SEL_DEACTIVATE:
                        showListB(五);
                        打破;
                    }
                }                公共无效onNothingSelected(适配器视图<>为arg0)
                {
                    // TODO自动生成方法存根
                }
        });        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT; (这一点,android.R.layout.simple_spinner_item,的ActionList);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(适配器);        rowAction.addView(tvAction);
        rowAction.addView(微调);        table.addView(rowAction,新TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    }    ...
}

这code正常工作。当无论是从列表中选择激活或停用, showListB()执行这非常类似于 showListA()在如何创建一个新行,其中包含标签和微调。

问题是,默认情况下,激活,其中执行微调显示 showListB()键,马上蝙蝠,表格的第二部分是创建基于激活选项。我能想出的唯一解决方法是将第三个字段添加到微调,像这样:

 私有静态最后的String [] =的ActionList {无,激活,停用};...开关(位置)
{
案例SEL_NONE:
    打破;
案例SEL_ACTIVATE:
案例SEL_DEACTIVATE:
    showListB(五);
    打破;
}

这工作......但我不希望在列表中的第三个选项。我只是希望它在默认情况下是空的或显示一些提示文本不在列表中的一个选项,一旦它是pressed。这可能吗?

感谢

编辑:

XML内容:

 <微调
    机器人:ID =@ + ID / spinnerAction
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/>


解决方案

我的数据sizes.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<资源>
    <字符串数组名=豆腐块>
        <项目> 2'; /项目>
        <项目> 4℃; /项目>
        <项目> 8示/项目>
        <项目> 16 LT; /项目>
        <项目> 32 LT; /项目>
    < /字符串数组>
< /资源>

在main.xml中:

 <微调机器人:ID =@ + ID / spinnerSize
机器人:layout_marginLeft =50像素
机器人:layout_width =FILL_PARENT
机器人:drawSelectorOnTop =真
机器人:layout_marginTop =5dip
机器人:提示=@字符串/ SelectSize
机器人:layout_marginRight =30像素
机器人:layout_height =35px/>

在Java的code:

 微调spinnerSize;
ArrayAdapter适配器;...spinnerSize =(微调)findViewById(R.id.spinnerSize);
适配器= ArrayAdapter.createFromResource(这一点,R.array.chunks,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSize.setAdapter(适配器);
spinnerSize.setOnItemSelectedListener(新MyOnItemSelectedListener());...类MyOnItemSelectedListener实现OnItemSelectedListener {    公共无效onItemSelected(适配器视图<>母公司,
        查看查看,INT POS,长I​​D){
        CHUNKSIZE =新的整数(parent.getItemAtPosition(POS)的ToString())的intValue()。
    }
    公共无效onNothingSelected(适配器视图<>母公司){
      // 假
    }
}

所以,虽然我可以看到2作为我的第一个默认的项目,什么都不会发生,除非用户实际选择它。

希望这有助于!

I have an Android form that needs to update itself based on certain selections. The form is currently made up of 2 Spinners (A and B). Spinner B not created until Spinner A's selection is made. After the selection is made B will be displayed to the view and it's contents dynamically filled based on A's selection. Here is my code:

public class MyForm extends Activity 
{   
    private final int SEL_ACTIVATE = 0;
    private final int SEL_DEACTIVATE = 1;

    private static final String[] actionList = {"Activate", "Deactivate" };

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

        table = (TableLayout) findViewById(R.id.table);

        showListA(table);
    }

    public void showListA(View v)
    {        
        rowAction = new TableRow(this);

        Spinner spinner = new Spinner(this);
        spinner.setPrompt("Select...");
        spinner.setOnItemSelectedListener(
            new OnItemSelectedListener() 
            {
                public void onItemSelected(AdapterView<?> parent, View v, int position, long id) 
                {
                    switch (position)
                    {
                    case SEL_ACTIVATE:
                    case SEL_DEACTIVATE:
                        showListB(v);
                        break;
                    }
                }

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, actionList);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(adapter);

        rowAction.addView(tvAction);
        rowAction.addView(spinner);

        table.addView(rowAction, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

    ...
}

This code works correctly. When either "Activate" or "Deactivate" are selected from the list, showListB() executes which is very similar to showListA() in how it creates a new row which contains Label and Spinner.

The problem is that, by default, "Activate" is shown in the Spinner which executes showListB() and right off the bat, the second part of the form is created based on the "Activate" option. The only workaround that I can come up with is to add a third field to the Spinner like so:

private static final String[] actionList = {"None", "Activate", "Deactivate" };

...

switch (position)
{
case SEL_NONE:
    break;
case SEL_ACTIVATE:
case SEL_DEACTIVATE:
    showListB(v);
    break;
}

This works... but I don't want a third option in the list. I just want it to, by default, be blank or show some sort of 'prompt' text that is not an option in the list once it is pressed. Is this possible?

Thanks

EDIT:

xml content:

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

解决方案

My data-sizes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="chunks">
        <item>2</item>
        <item>4</item>
        <item>8</item>
        <item>16</item>
        <item>32</item>
    </string-array>
</resources>

In main.xml:

<Spinner android:id="@+id/spinnerSize"  
android:layout_marginLeft="50px"
android:layout_width="fill_parent"                  
android:drawSelectorOnTop="true"
android:layout_marginTop="5dip"
android:prompt="@string/SelectSize"
android:layout_marginRight="30px"
android:layout_height="35px" /> 

In Java Code:

Spinner spinnerSize;
ArrayAdapter adapter;

...

spinnerSize = (Spinner)findViewById(R.id.spinnerSize);
adapter = ArrayAdapter.createFromResource(this, R.array.chunks, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSize.setAdapter(adapter);
spinnerSize.setOnItemSelectedListener(new MyOnItemSelectedListener());

...

class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
        chunkSize = new Integer(parent.getItemAtPosition(pos).toString()).intValue();
    }
    public void onNothingSelected(AdapterView<?> parent) {
      // Dummy
    }
}

So, although I can see 2 as my first default item, nothing happens unless user actually selects it.

Hope this helps!

这篇关于Android的微调 - 如何的默认列表选择为无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆