OnClickListener只启动了一个函数

时间:2018-04-03 03:38:33

标签: java android onclicklistener android-datepicker

这是我的观点,其中我有星期一到星期日的表格,其中包含我有日期的文字视图

<?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>

以下是我对该视图的活动

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

所以我的问题是当我第一次触摸/点击textview时“toast”启动了datepicker没有显示,但是之后每当我触摸/点击时我都能看到datepicker?当我第一次按textview时为什么没有出现datepicker?如何使它起作用?

vikas kumar感谢帮助

2 个答案:

答案 0 :(得分:1)

原因是你第一次点击它时设置了点击监听器,因为你在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设置两个onClickListeners。

答案 1 :(得分:0)

将您的setDate()方法修改为以下

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