Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 允许用户在Spinner中输入新值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我是 android开发的新手.我正在构建一个小应用程序,其中包含一个微调器,其中包含四个数字的预定义set(数组),我使用ArrayAdapter将值从资源文件提供给微调器.

微调器工作正常,用户可以选择值.但我也希望用户能够输入新值,如果他们想输入新值.我该怎么做?

onCreate活动方法中的代码

Spinner spinner =(Spinner)findViewById(R.id.spinner);
        //使用字符串数组和认的微调器布局创建ArrayAdapter
        arrayAdapter<&CharSequence的GT; adapter = ArrayAdapter.createFromresource(这个,
        r.array.points_preset,android.R.layout.simple_spinner_item);
        //指定出现选项列表时要使用的布局
        adapter.setDropDownViewresource(android.R.layout.simple_spinner_dropdown_item);
        //将适配器应用于微调器
        spinner.setAdapter(适配器);
        spinner.setOnItemSELEctedListener(new SpinnerActivity());

SpinnerActivity类的代码

更新以包含输入对话框:

public class SpinnerActivity extends Activity implements OnItemSELEctedListener {

        public void onItemSELEcted(AdapterView<?> parent,final View view,int pos,long id) {
            if (pos==3)
            {
                // Set an EditText view to get user input 
                final EditText input = new EditText(MainActivity.this);

                new AlertDialog.builder(MainActivity.this)
                                            .setmessage("Enter your Point here")
                    .setView(input)
                    .setPositiveButton("Ok",new DialogInterface.onClickListener() {
                         public void onClick(DialogInterface dialog,int whichButton) {
                          Editable   editable = input.getText(); 
                          //if I uncomment following line,the application terminates
                       // Spinner spinner = (Spinner) findViewById(R.id.spinner);

                         }
                    })
                    .setNegativeButton("Cancel",int whichButton) {
                                // Do nothing.
                         }
                    }).show(); 
            }

                                    }

        public void onNothingSELEcted(AdapterView<?> parent) {
            // Another interface callBACk
        }
    }
                    `

Strings.xml资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <String name="app_name">PointCalculator</String>
    <String name="menu_setTings">SetTings</String>
    <String-array name="points_preset">
    <item>3</item>
    <item>10</item>
    <item>0</item>
    <item>Oth</item>
    </String-array>

</resources>

SpinnerActivity类的更新和工作版本

`

public class SpinnerActivity extends Activity implements OnItemSELEctedListener {

                public void onItemSELEcted(AdapterView<?> parent,long id) {
                    if (pos==3)
                    {
                        // Set an EditText view to get user input 
                        final EditText input = new EditText(MainActivity.this);

                        new AlertDialog.builder(MainActivity.this)
                                                    .setmessage("Enter your Point here")
                            .setView(input)
                            .setPositiveButton("Ok",new DialogInterface.onClickListener() {
                                 public void onClick(DialogInterface dialog,int whichButton) {
                                  Editable   editable = input.getText(); 


                           arrayList.add(editable.toString());
                            adapter.notifyDataSetChanged();


                                 }
                            })
                            .setNegativeButton("Cancel",int whichButton) {
                                        // Do nothing.
                                 }
                            }).show(); 
                    }

                                            }

                public void onNothingSELEcted(AdapterView<?> parent) {
                    // Another interface callBACk
                }
            }`

谢谢,

解决方法

您创建ArrayAdapter的方式将不允许您非常轻松地添加到阵列,因为将从资源创建支持阵列.

首先从资源加载数组并将其添加到List.然后使用该List创建ArrayAdapter.

然后,您可以通过添加支持列表将项添加到微调器.您需要在适配器上调用notifyDatasetChanged(),让它知道您更改了适配器中的可用项.

大佬总结

以上是大佬教程为你收集整理的android – 允许用户在Spinner中输入新值全部内容,希望文章能够帮你解决android – 允许用户在Spinner中输入新值所遇到的程序开发问题。

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

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