开发者

Picker for characters in android

I'm a beginner in the android development, so I would like to ask you , How to create picker for letter开发者_开发知识库s of alphabet ?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜