我是android开发的初学者,所以我想问你,如何为字母创建选择器?
问问题
2738 次
3 回答
7
我最近试图弄清楚这一点,您可以使用 NumberPicker 小部件来做到这一点。
在您的 XML 中定义您的号码选择器
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/NumberPicker"/>
</LinearLayout>
然后在您的 onCreate 方法中创建一个 NumberPicker 并转换 XML 布局,然后使用 NumberPicker 方法设置您的数据。
下面使用的方法创建要在选择器中显示的项目数组。最小值和最大值创建数字的范围。如果范围与数组中的项目数不同,它将不会运行。因此,如果它的完整字母表执行 0 和 25。 setDisplayedValues 方法会覆盖您使用数组中的相应值(0、1、2 等)定义的数字
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
NumberPicker NumberPicker = (NumberPicker) findViewById(R.id.NumberPicker);
String[] alphabet = {"A","B","C","D","E","F","G"}; //etc
NumberPicker.setMaxValue(0);
NumberPicker.setMaxValue(6);
NumberPicker.setDisplayedValues(alphabet);
}
我知道这个问题是从 3 年前开始的......哦,好吧......希望你还没有被困住,也许这会帮助别人。
于 2014-09-19T04:22:58.727 回答
1
我想你想要一个 Spinner。您应该参考本指南了解如何制作。
于 2011-08-09T17:40:46.297 回答
0
如果您不想使用微调器,并且出于某种原因希望让用户单击每个字母,则可以将其设置为:
TextView txt = (EditText)findViewById(R.id.pickertxt);
Button next = (Button)findViewById(R.id.nextbtn);
Button prev = (Button)findViewById(R.id.prevbtn);
char picker = 'a';
txt.setText(picker);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
picker++;
txt.setText(picker);
}
});
prev.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
picker--;
txt.setText(picker);
}
});
虽然使用微调器可能是首选(它更像是一个下拉列表,您可以从列表中选择值,而不是单击)或者如果您正在做 AZ,为什么不只是一个允许用户输入的 EditText 呢?
于 2011-08-09T18:30:40.703 回答