-6
我想知道是否可以使NumberPicker中的文字可點擊並且在單擊白色區域或文本區域時表現相同。我之所以嘗試做這樣的事情,是因爲當我在酒吧中有很長的字符串時,選擇該區域變得非常困難。我附上我的代碼到目前爲止。如何使NumberPicker中的文字可點擊
MainActivity.java
package com.yardimobileinterns.numberpicker;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private String[] values = {"University1", "University2", "University3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setMovementMethod(null);
editText.setSelection(editText.getText().length());
showDialog(editText, values);
}
});
}
public void showDialog(EditText editText, String[] values) {
EditTextDialogFragment editTextDialogFragment = EditTextDialogFragment.getNewInstance(editText,values);
editTextDialogFragment.show(getFragmentManager(), "dialog");
}
}
EditTextDialogFragment.java
package com.yardimobileinterns.numberpicker;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.NumberPicker;
public class EditTextDialogFragment extends DialogFragment {
public static final String EDIT_TEXT = "EDIT_TEXT";
public static final String VALUES = "VALUES";
private String[] mValues;
private NumberPicker numberPicker;
private EditText mEditText;
private String selectedValue;
public EditTextDialogFragment() {
}
public static EditTextDialogFragment getNewInstance(EditText editText, String[] values) {
EditTextDialogFragment editTextDialogFragment = new EditTextDialogFragment();
Bundle bundle = new Bundle();
bundle.putStringArray(VALUES, values);
editTextDialogFragment.setEditText(editText);
editTextDialogFragment.setArguments(bundle);
return editTextDialogFragment;
}
public void setEditText(EditText editText) {
mEditText = editText;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mValues = getArguments().getStringArray(VALUES);
}
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = layoutInflater.inflate(R.layout.university_picker, viewGroup, false);
getDialog().getWindow().setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM);
numberPicker = (NumberPicker) view.findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
selectedValue = (mValues.length == 0) ? "" : mValues[0];
numberPicker.setMaxValue(mValues.length - 1);
numberPicker.setDisplayedValues(mValues);
numberPicker.setWrapSelectorWheel(false);
numberPicker.setClickable(true);
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
selectedValue = mValues[newVal];
}
});
numberPicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
mEditText.setText(selectedValue);
}
});
return view;
}
}
university_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yardimobileinterns.numberpicker.MainActivity"
android:background="#ffffff">
<NumberPicker
android:id="@+id/numberPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
>
</NumberPicker>
</RelativeLayout>
我想使用SpannableString,但在NumberPicker中,我只能使用int或String作爲值。 –