我想知道是否可以使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>
答案 0 :(得分:0)
我找到的实际解决方案是使用onTouch模拟onClick,以便允许单击文本。它现在运作良好。
package com.yardimobileinterns.numberpicker;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
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;
private int lastAction = -1;
public EditTextDialogFragment() {
}
public static EditTextDialogFragment getNewInstance(EditText editText, String[] values) {
EditTextDialogFragment editTextDialogFragment = new EditTextDialogFragment();
editTextDialogFragment.setValues(values);
editTextDialogFragment.setEditText(editText);
return editTextDialogFragment;
}
public void setEditText(EditText editText) {
mEditText = editText;
}
public void setValues(String[] values) {
mValues = values;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@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[mValues.length / 2];
numberPicker.setMaxValue(mValues.length - 1);
numberPicker.setDisplayedValues(mValues);
numberPicker.setValue(mValues.length / 2);
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.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean solution = false;
switch (event.getAction()) {
case (MotionEvent.ACTION_DOWN):
lastAction = MotionEvent.ACTION_DOWN;
solution = false;
break;
case (MotionEvent.ACTION_UP):
if (lastAction == MotionEvent.ACTION_DOWN) {
solution = true;
}
lastAction = MotionEvent.ACTION_UP;
break;
case (MotionEvent.ACTION_SCROLL):
lastAction = MotionEvent.ACTION_SCROLL;
solution = false;
break;
default:
lastAction = -1;
solution = false;
break;
}
if (solution) {
getDialog().dismiss();
mEditText.setText(selectedValue);
}
return solution;
}
});
return view;
}
}