大佬教程收集整理的这篇文章主要介绍了android – 允许用户在Spinner中输入新值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
微调器工作正常,用户可以选择值.但我也希望用户能够输入新值,如果他们想输入新值.我该怎么做?
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 } } `
<?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 } }`
谢谢,
以上是大佬教程为你收集整理的android – 允许用户在Spinner中输入新值全部内容,希望文章能够帮你解决android – 允许用户在Spinner中输入新值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。