首先,我是
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
}
}`
谢谢,