Bis*_*del 4 android android-widget
首先,我是android开发的新手。我正在构建一个小型应用程序,它的 UI 中包含一个微调器,该应用程序具有四个数字的预定义集合(数组),我正在使用ArrayAdapter该值从资源文件中将值提供给微调器。
微调器工作正常,用户可以选择值。但我也希望用户能够在他们希望输入新值时输入新值。我该怎么做?
Activity 的 onCreate 方法中的代码:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.points_preset, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
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
}
}
`
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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
}
}`
Run Code Online (Sandbox Code Playgroud)
谢谢,
有几种方法可以做到这一点。我们这样做的一种方法是让一个spinner项目说“添加项目”。在onItemSelected我们有一个AlertDialog弹出的EditText用于输入项目然后你只需将它添加到你的Array或你正在使用的任何东西中。您也EditText可以在Spinner或 任何地方旁边添加一个新项目,然后当他们单击 a 时,Button您将其添加到存储数据的任何位置并调用notfiyDataSetChanged()以更新Spinner. 我希望这回答了你的问题
创建 ArrayAdapter 的方式不允许您很容易地添加到数组,因为支持数组将从资源创建。
首先从资源中加载数组并将其添加到列表中。然后使用该列表创建 ArrayAdapter。
然后,您可以通过添加到支持列表来将项目添加到微调器。您需要在适配器上调用notifyDatasetChanged(),以使其知道您更改了适配器中的可用项目。
| 归档时间: |
|
| 查看次数: |
4112 次 |
| 最近记录: |