如何在TextViiew上添加datepicker onclick。表单中有多个日期字段

时间:2016-05-30 02:27:49

标签: android android-layout android-fragments datepicker

我对Android编程完全陌生,很抱歉,如果我问一个愚蠢的问题。如何在TextView上添加datepicker onclick然后选择它应该更新相应TextViews的日期。我在表单中有多个日期字段。并且在其他片段中也有许多日期字段。我见过很多答案,但不知道如何应用它。我的意思是哪个代码在哪里。请好好解释一下。

更新 我现在能够通过不同的TextViews来点击日期选择器,但是只有一个TextView会在选择日期时更新,而不是在相应的TextView上。

XML:

 <TextView
        android:id="@+id/DateOfEventForm"
        android:textSize="15dp"
        android:background="@drawable/roundwhite"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_gravity="center_horizontal"
        android:textColor="@android:color/black" />
 <TextView
        android:id="@+id/TimeDateOccurence"
        android:layout_width="match_parent"
        android:textSize="15dp"
        android:background="@drawable/roundwhite"
        android:layout_height="38dp"
        android:layout_gravity="center_horizontal"
        android:textColor="@android:color/black" />

这就是活动片段现在的样子

public class FragmentReportAFault extends Fragment {
private View mRootView;
private Button mNextButton, mSaveButton, mDeleteButton;
private LinearLayout mFirstLayoutForm, mSecondLayoutForm;
private TextView mSelectorOne, mSelectorTwo;
private TextView DateOfEventForm,TimeDateOccurence;
int clicked;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.fragment_reportafault, container, false);
    uiInitialize();
    setListners();
    return mRootView;
}


private void uiInitialize() {
    mFirstLayoutForm = (LinearLayout) mRootView.findViewById(R.id.FirstLayoutForm);
    mSecondLayoutForm = (LinearLayout) mRootView.findViewById(R.id.SecondLayoutForm);
    mNextButton = (Button) mRootView.findViewById(R.id.btnNext);
    mSaveButton = (Button) mRootView.findViewById(R.id.btnSave);
    mDeleteButton = (Button) mRootView.findViewById(R.id.btnDelete);
    mSelectorOne = (TextView) mRootView.findViewById(R.id.selector1);
    mSelectorTwo = (TextView) mRootView.findViewById(R.id.selector2);
    DateOfEventForm= (TextView)mRootView.findViewById(R.id.DateOfEventForm);
    TimeDateOccurence= (TextView)mRootView.findViewById(R.id.TimeDateOccurence);
}
private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        if (clicked == 0) {
            DateOfEventForm.setText(year + " -" + (monthOfYear + 1) + "-" + dayOfMonth);
        }
        else if (clicked == 1){
            DateOfEventForm.setText(year + " -" + (monthOfYear + 1) + "-" + dayOfMonth);

        }
    }
};

private void setListners() {
    mNextButton.setOnClickListener(onClick);
    mSaveButton.setOnClickListener(onClick);
    mDeleteButton.setOnClickListener(onClick);
    mFirstLayoutForm.setVisibility(View.VISIBLE);
    mSecondLayoutForm.setVisibility(View.GONE);
    mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
    mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner_white);
    DateOfEventForm.setOnClickListener(onClick);
    TimeDateOccurence.setOnClickListener(onClick);
}

View.OnClickListener onClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btnNext:{
                mFirstLayoutForm.setVisibility(View.GONE);
                mSecondLayoutForm.setVisibility(View.VISIBLE);
                mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
                mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner);
                mSelectorTwo.setTextColor(Color.WHITE);

            }break;
            case R.id.btnSave:{

            }break;
            case R.id.btnDelete:{

            }
            break;
            case R.id.DateOfEventForm:{

                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                clicked =0;

                DatePickerDialog dateDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
                dateDialog.show();
            }
            break;
            case R.id.TimeDateOccurence:{

                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                clicked =1;

                DatePickerDialog dateDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
                dateDialog.show();
            }
            break;

        }
    }
};}

1 个答案:

答案 0 :(得分:1)

对代码进行了更改。我没有测试过。试试这个

    private View mRootView;
    private Button mNextButton, mSaveButton, mDeleteButton;
    private LinearLayout mFirstLayoutForm, mSecondLayoutForm;
    private TextView mSelectorOne, mSelectorTwo;
    int clicked;

    //added
    private TextView DateOfEventForm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_reportafault, container, false);
        uiInitialize();
        setListners();
        return mRootView;
    }


    private void uiInitialize() {
        mFirstLayoutForm = (LinearLayout) mRootView.findViewById(R.id.FirstLayoutForm);
        mSecondLayoutForm = (LinearLayout) mRootView.findViewById(R.id.SecondLayoutForm);
        mNextButton = (Button) mRootView.findViewById(R.id.btnNext);
        mSaveButton = (Button) mRootView.findViewById(R.id.btnSave);
        mDeleteButton = (Button) mRootView.findViewById(R.id.btnDelete);
        mSelectorOne = (TextView) mRootView.findViewById(R.id.selector1);
        mSelectorTwo = (TextView) mRootView.findViewById(R.id.selector2);
        DateOfEventForm= (TextView)mRootView.findViewById(R.id.DateOfEventForm);

    }

    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            if(clicked==0) {
              DateOfEventForm.setText(year +" -"+(monthOfYear+1)+ "-"+dayOfMonth);
            }        
   }
    };

    private void setListners() {
        mNextButton.setOnClickListener(onClick);
        mSaveButton.setOnClickListener(onClick);
        mDeleteButton.setOnClickListener(onClick);
        DateOfEventForm.setOnClickListener(onClick);
        mFirstLayoutForm.setVisibility(View.VISIBLE);
        mSecondLayoutForm.setVisibility(View.GONE);
        mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
        mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner_white);
    }

    View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btnNext:{
                    mFirstLayoutForm.setVisibility(View.GONE);
                    mSecondLayoutForm.setVisibility(View.VISIBLE);
                    mSelectorOne.setBackgroundResource(R.drawable.rounded_corner);
                    mSelectorTwo.setBackgroundResource(R.drawable.rounded_corner);
                    mSelectorTwo.setTextColor(Color.WHITE);

                }break;
                case R.id.btnSave:{

                }break;
                case R.id.btnDelete:{

                }
                break;
               case R.id.DateOfEventForm:{

                      Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

         clicked =0;
        DatePickerDialog dateDialog = new DatePickerDialog(getActivity(),      dateSetListener, year, month, day);
        dateDialog.show();
                }
                break;

            }
        }
    }; }