Android TextView 在第二次点击后打开日期选择器

Android TextView opening datepicker after second click

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:paddingTop="10dp">

                    <TextView
                        android:id="@+id/monday"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_marginRight="15dp"
                        android:layout_weight="1"
                        android:gravity="center|top"
                        android:text="@string/monday"
                        android:textAlignment="center"
                        android:textColor="@color/textColor"
                        android:textSize="24sp"
                        android:textStyle="bold" />
                </LinearLayout>
           </LinearLayout>
       </LinearLayout>
   </HorizontalScrollView>

我有:

private int fillFields(int id, String data, int counter){
    ViewGroup timeList = findViewById(id);
    for(int i = 0; i < 5; i++) {
        ViewGroup firstModayTimeListChild = (ViewGroup) timeList.getChildAt(i);
        ViewGroup period = (ViewGroup) firstModayTimeListChild.getChildAt(1);
        TextView periodFromText = (TextView) period.getChildAt(0);
        TextView periodToText = (TextView) period.getChildAt(2);

        periodFromText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        periodToText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);


        periodFromText.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                setDate(view);
            }
        });


        periodToText.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                setDate(view);
            }
        });

        periodFromText.setText(
                String.format(
                        data.charAt(++counter) + "" +
                                data.charAt(++counter) + ":" +
                                data.charAt(++counter) + "" +
                                data.charAt(++counter)
                ));

        periodToText.setText(
                String.format(
                        data.charAt(++counter) + "" +
                                data.charAt(++counter) + ":" +
                                data.charAt(++counter) + "" +
                                data.charAt(++counter)
                ));
    }
    return counter;
}
 public void setDate(View view){
    selectedTextView = (TextView) view;
    view.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            showDialog(0);
        }
    });
}

protected Dialog onCreateDialog(int id){
    Calendar now = Calendar.getInstance();

    TimePickerDialog timePickerDialog = new TimePickerDialog(this, TimePickerDialog.THEME_HOLO_DARK, timePickerListener, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), true);
    return timePickerDialog;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
        selectedTextView.setText(String.format("%02d:%02d", hour, minute));
    }
};

我尝试实现的是,在单击 TextView 后想要打开 DatePicker 并且几乎打开了,我的意思是我的代码正在运行,但我需要触摸 TextView 两次,因为在一次之后没有任何反应。

我正在寻找解决 google 中的问题,但我发现与 "focusable" 相关,但没有帮助。

periodFromText.focusable(假); periodFromText.focusableInTouchMode(假);

没有帮助,知道吗?一些提示会很好。

好的,感谢 Jyoti JK 我找到了解决方案,我误解了 "focusable" 我将其设置为 "false" 而不是 "true",现在我实际上设置了 [=31] =] 在 "true" 上一切正常。谢谢

您可以尝试在文本视图中添加这个

  android:focusableInTouchMode="false"
  android:clickable="true"

或改用按钮。

尝试设置textview

    android:inputType="none"