android spinner输入,android – 允许用户在Spinner中输入新值

首先,我是

android开发的新手.我正在构建一个小应用程序,其中包含一个微调器,其中包含四个数字的预定义set(数组),我使用ArrayAdapter将值从资源文件提供给微调器.

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

onCreate活动方法中的代码:

Spinner spinner =(Spinner)findViewById(R.id.spinner);

//使用字符串数组和默认的微调器布局创建ArrayAdapter

ArrayAdapter

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", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

// Do nothing.

}

}).show();

}

}

public void onNothingSelected(AdapterView> parent) {

// Another interface callback

}

}

`

Strings.xml资源文件

PointCalculator

Settings

3

10

0

Oth

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();

arrayList.add(editable.toString());

adapter.notifyDataSetChanged();

}

})

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

// Do nothing.

}

}).show();

}

}

public void onNothingSelected(AdapterView> parent) {

// Another interface callback

}

}`

谢谢,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值