Picker for characters in android

蓝咒 提交于 2019-12-04 22:04:43
CharlesCates

I was recently trying to figure this out, and you can do it by using the NumberPicker widget.

In your XML define your number picker

<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>

Then in your onCreate method create a NumberPicker and cast the XML layout, then set your data using the NumberPicker methods.

The methods used below create the array of items to be displayed in the picker. The min and max value create the range for numbers. If the range isn't the same number of items in your array it won't run. So if its the full alphabet do 0 and 25. The setDisplayedValues method overrides the numbers you defined with the corresponding values in the array (0, 1, 2 etc)

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

I know this question was from 3 years ago....oh well...hope you're still not stuck and maybe this will help someone else out.

also this Using NumberPicker Widget with Strings

I think you want a Spinner. You should refer to this guide for how to make one.

If you don't want to use a spinner and would, for some reason, prefer to have the user click through each letter, you could set it up something like this:

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

Although using a spinner would probably be preferred (it's more like a dropdown where you can select the value from a list, rather than clicking through) or if you're doing A-Z, why not just an EditText that lets a user type it in?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!