OnClickListener 只有一个功能被触发

OnClickListener only one function is fired up

这是我的视图,其中有周一至周日的表格,其中有文本视图,其中有日期

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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>

                    <ScrollView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <LinearLayout
                            android:id="@+id/mondayTimesList"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 1"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:hint="From"
                                        android:padding="10dp" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 2"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="From" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 3"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="From" />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                      android:layout_width="100dp"
                                      android:layout_height="wrap_content"
                                      android:padding="10dp"
                                      android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 4"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                      android:layout_width="100dp"
                                      android:layout_height="wrap_content"
                                      android:padding="10dp"
                                      android:hint="From" />

                                    <TextView
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"
                                      android:layout_marginLeft="10dp"
                                      android:layout_marginRight="10dp"
                                      android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:padding="10dp">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:background="@color/orangeFlat"
                                    android:padding="5dp"
                                    android:text="Period 5"
                                    android:textColor="@color/primary" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="center">

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="From" />

                                    <TextView
                                       android:layout_width="wrap_content"


                                      android:layout_height="wrap_content"
                                        android:layout_marginLeft="10dp"
                                        android:layout_marginRight="10dp"
                                        android:text=":" />

                                    <TextView
                                        android:layout_width="100dp"
                                        android:layout_height="wrap_content"
                                        android:padding="10dp"
                                        android:hint="To" />
                                </LinearLayout>
                            </LinearLayout>
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/primary"
        >
        <LinearLayout
            android:id="@+id/back"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/arrow"
                android:rotation="180"
                android:layout_weight="1"
                android:onClick="backToHomeScreen"
                android:background="@color/orangeFlat"
                android:paddingVertical="10dp"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/save"
                android:layout_weight="1"
                android:onClick="saveAsTemplate"
                android:background="@color/orangeFlat"
                android:paddingVertical="10dp"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/update"
                android:layout_weight="1"
                android:onClick="updateTimming"
                android:background="@color/orangeFlat"
                android:paddingVertical="10dp"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

这是我的 activity 视图

    public class TimesheetActivity extends AppCompatActivity {
    TextView selectedTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timming_list);

        Intent data = getIntent();
        String times = data.getStringExtra("Times");

        int counter = 1;
        counter = fillFields(R.id.sundayTimesList, times, counter);
        counter = fillFields(R.id.mondayTimesList, times, counter);
        counter = fillFields(R.id.tuesdayTimesList, times, counter);
        counter = fillFields(R.id.wednesdayTimesList, times, counter);
        counter = fillFields(R.id.thursdayTimesList, times, counter);
        counter = fillFields(R.id.fridayTimesList, times, counter);
        fillFields(R.id.saturdayTimesList, times, counter);

    }

    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.setClickable(true);
            periodFromText.setFocusableInTouchMode(false);

            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){
                    Toast.makeText(getApplicationContext(), "WORKS!", Toast.LENGTH_SHORT).show();
                    setDate(view);
                }
            });

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

    //            Log.i("FOCUSABLE ", "" + periodFromText.isFocusable());
    //            Log.i("FOCUSABLE TOUCH MODE ", "" + periodFromText.isFocusableInTouchMode());
    //            Log.i("IS CLICKABLE ", "" + periodFromText.isClickable());

            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));
        }
    };
    }

所以我的问题是当我 touch/click textview 第一次 "toast" 启动日期选择器时没有显示,但之后每次 touch/click 我都能看到日期选择器?为什么我第一次按 textview 时没有显示日期选择器?如何让它发挥作用?

vikas kumar 感谢您的帮助

修改你的setDate()方法如下

 public void setDate(View view){
        selectedTextView = (TextView) view;
        showDialog(0);
    }

原因是当您第一次点击时,它会像您在 setDate(View view); 中设置的那样设置点击侦听器,之后,它会侦听后续调用。所以你需要稍微改变一下方法。

public void setDate(View view){
 selectedTextView = (TextView) view;
 showDialog(0);
}

或者您也可以在日期选择器的 onClick 站点上执行此操作。

 periodFromText.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Toast.makeText(getApplicationContext(), "WORKS!", Toast.LENGTH_SHORT).show();
                selectedTextView = (TextView) view;
                showDialog(0);
            }
        });

这不需要您为同一个 TextView 设置两个 onClickListener。