我想在TimePicker上禁用键盘输入。你有想法解决这个问题吗?因为它很烦人。
提前致谢!
顺便说一下,我尝试了这个非常受欢迎的解决方案setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
,但它对我的代码无效。
这是我的代码;
MainActivity.java
public void selectTimeToDisplay(View view) {
DialogFragment dialogFragment = new DisplayTimeFragment();
dialogFragment.show(getSupportFragmentManager(), "DatePicker");
}
public class DisplayTimeFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c= Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
view.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);//doesnt work
setSelectedTime(hourOfDay,minute);
}
public void setSelectedTime(int hourOfDay,int minute) {
saat1.setText(hourOfDay+":"+minute);}
activity_main.xml中
<EditText
android:layout_width="70dp"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="@+id/bolme1saat"
android:layout_gravity="center_horizontal"
android:onClick="selectTimeToDisplay"
android:layout_above="@+id/textView4"
android:layout_alignStart="@+id/bolme2saat"/>
答案 0 :(得分:0)
如果您不在此课程中使用键盘,则只需禁用键盘
即可 <activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />
替代方案可以强制android隐藏键盘
// This works 100%
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
答案 1 :(得分:0)
您可以在xml中尝试android:focusable =“false”和android:clickable =“true”。