Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何设置当前微调文本而不更改关联的选择列表项大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个微调器有一些价值观
| Monday       | 
| Thuesday     |
| Wednesday    |
| Thursday     |
| Friday       |
| Saturday     |
| USER DEFINED |

用户选择USER DEFINED时,他可以在对话框中输入自定义值,假设我将该值设为String userDef =“Your choice”.

我需要将此String设置为当前项,而不更改微调选择列表,必须与上述相同,当用户再次点击微调器时,如Google Analytics(分析)Android应用程序中的内容,请参阅该图像.

未点击的微调

点击微调

我该怎么做?

解决方法

实现这一点的关键细节是Spinner使用的 SpinnerAdapter界面有两种不同但相关的方法

> getView() – 创建Spinner本身显示的视图.
> getDropDownView() – 创建下拉弹出窗口中显示的视图.

因此,要使一个在弹出窗口本身不同弹出窗口中显示的项目,您只需要以不同的方式实现这两种方法.根据您的代码的细节,细节可能会有所不同,但一个简单的例子就是:

public class AdapterWithCustomItem extends ArrayAdapter<String>
{
    private final static int position_user_DEFINED = 6;
    private final static String[] OPTIONS = new String[] {
        "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Custom..." };

    private String mCustomText = "";

    public AdapterWithCustomItem(Context context){
        super(context,android.R.layout.simple_spinner_dropdown_item,OPTIONS);
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        View view = super.getView(position,convertView,parent);
        if (position == POSITION_user_DEFINED) {
            TextView tv = (TextView)view.findViewById(android.R.id.text1);
            tv.setText(mCustomText);
        }

        return view;
    }

    public void setCustomText(String customText) {
        // Call to set the text that must be shown in the spinner for the custom option.
        mCustomText = customText;
        notifyDataSetChanged();
    }

    @Override
    public View getDropDownView(int position,ViewGroup parent) {
        // No need for this override,actually. It's just to clarify the difference.
        return super.getDropDownView(position,parent);
    }
}

然后,当用户输入自定义值时,您只需要使用要显示的文本调用适配器上的setCustomText()方法即可.

@H_912_2@mAdapter.setCustomText("This is displayed for the custom option");

产生以下结果:

由于您只是覆盖了getView()方法,所以下拉列表仍然显示与选项本身中定义的文本相同的文本.

大佬总结

以上是大佬教程为你收集整理的android – 如何设置当前微调文本而不更改关联的选择列表项全部内容,希望文章能够帮你解决android – 如何设置当前微调文本而不更改关联的选择列表项所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。