使用DialogFragment和Android注释设置日期 [英] Setting date using DialogFragment and Android Annotations

查看:60
本文介绍了使用DialogFragment和Android注释设置日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写我在带有对话框片段的DatePicker中选择的日期时遇到问题.我正在尝试使用Android注释来做到这一点.问题是,我必须实现在DatePickerFragment类的OnDateSet函数上将此日期设置为TextView的行为.

I have problem with writing date I selected in DatePicker with Dialog Fragment. I`m trying to do this using Android Annotations. Problem is, I have to implement behavior for setting this date to TextView on OnDateSet function in DatePickerFragment class.

我该如何选择应该在哪个TextView中编写哪个@Click?

How can I choose in which TextView, which @Click should write?

以下代码:

@EActivity(R.layout.activity_main_search)public class SearchActivity extends FragmentActivity{

private int mFYear, mFMonth, mFDay, mTYear, mTMonth, mTDay;

 (R.id.main_search_city)
EditText cityET;

@ViewById(R.id.main_search_price)
EditText priceET;

@ViewById(R.id.main_search_from)
TextView dateFromTV;

@ViewById(R.id.main_search_to)
TextView dateToTV;

public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        }
}



@AfterViews
public void setCurrentdateonView(){
     final Calendar c = Calendar.getInstance();
     mFYear = c.get(Calendar.YEAR);
     mFMonth = c.get(Calendar.MONTH)+1;
     mFDay = c.get(Calendar.DAY_OF_MONTH);

     dateFromTV.setText(new StringBuffer().append(mFDay).append("-").append(mFMonth).append("-").append(mFYear).append(""));
     dateToTV.setText(new StringBuffer().append(mFDay).append("-").append(mFMonth).append("-").append(mFYear).append(""));                                 
 }   


@App
HApplication mApp;

@Click(R.id.main_search_from_btn)
public void showFromDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");


}

@Click(R.id.main_search_to_btn)
public void showToDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

@Click(R.id.main_search_btn)
public void openSearch(){
    //SearchActivity_.intent(this).city(cityET.getText().toString()).start();
    ListActivity_.intent(this).city(cityET.getText().toString()).price(priceET.getText().toString()).start();
}}

应该实现日期写的功能是:

Functions that should realise that date writing are:

@Click(R.id.main_search_from_btn)公共无效的showFromDatePickerDialog(View v)

@Click(R.id.main_search_from_btn) public void showFromDatePickerDialog(View v)

它应该写日期到dateFromTV

it should write date to dateFromTV

@Click(R.id.main_search_to_btn)公共无效的showToDatePickerDialog(View v)

@Click(R.id.main_search_to_btn) public void showToDatePickerDialog(View v)

它应该将date写入dateToTV

it should write date to dateToTV

我解决了这个问题.

我需要的是标志,这些标志设置在每个按钮@Click上,然后在onDateSet情况下进行标志号检查.也许会对某人有所帮助,因为我花了整整一天的时间才弄清楚...

What I needed was flags, which were set on each button @Click and then in onDateSet case switch with flag number check. Maybe it will help someone, because it took me whole day to figure it out...

推荐答案

Click注释仅用于触发对话框.您应该通过此处所述的侦听器接口实现回调方法: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

The Click annotation is just for triggering the dialog. You should implement a callback method through listener interfaces described here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

我为自己实现了一个版本,这是代码:

I implemented a version for myself, here is the code:

MainActivity.java:

MainActivity.java:

    package com.example.datepickerexample;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;

import com.example.datepickerexample.DatePickerFragment.DatePickerDialogListener;

public class MainActivity extends FragmentActivity implements
        DatePickerDialogListener {
    TextView lbl_from, lbl_to, cpn_from, cpn_to;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cpn_from = (TextView) findViewById(R.id.cpn_from);
        cpn_to = (TextView) findViewById(R.id.cpn_to);
        lbl_from = (TextView) findViewById(R.id.lbl_from);
        lbl_to = (TextView) findViewById(R.id.lbl_to);
    }

    public void showFromDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("isFromDate", true);
        newFragment.setArguments(bundle);
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

    public void showToDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("isFromDate", false);
        newFragment.setArguments(bundle);
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

    @Override
    public void onDatePicked(DialogFragment dialog, Calendar c,
            boolean isFromDate) {
        String strdate = null;

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

        if (c != null) {
        strdate = sdf.format(c.getTime());
        }
        if (isFromDate) {
            lbl_from.setText(strdate);
        } else {
            lbl_to.setText(strdate);
        }
    }

}

activity_main.xml:

activity_main.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/cpn_from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="6dp"
        android:text="@string/from_date" />

    <TextView
        android:id="@+id/lbl_from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/cpn_from"
        android:layout_toRightOf="@id/cpn_from" />

    <Button
        android:id="@+id/btn_from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cpn_from"
        android:onClick="showFromDatePickerDialog"
        android:text="@string/pick_from_date" />

    <TextView
        android:id="@+id/cpn_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_from"
        android:layout_margin="6dp"
        android:text="@string/to_date" />

    <TextView
        android:id="@+id/lbl_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_from"
        android:layout_alignBaseline="@id/cpn_to"
        android:layout_toRightOf="@id/cpn_to" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cpn_to"
        android:onClick="showToDatePickerDialog"
        android:text="@string/pick_to_date" />

</RelativeLayout>

DatePickerFragment.java:

DatePickerFragment.java:

package com.example.datepickerexample;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    public interface DatePickerDialogListener {
        public void onDatePicked(DialogFragment dialog, Calendar c,
                boolean isFromDate);
    }

    // Use this instance of the interface to deliver action events
    DatePickerDialogListener mListener;

    boolean isFromDate;

    public static DatePickerFragment newInstance(boolean isFromDate) {
        DatePickerFragment instance = new DatePickerFragment();

        instance = new DatePickerFragment();
        Bundle args = new Bundle();
        args.putBoolean("isFromDate", isFromDate);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            isFromDate = getArguments().getBoolean("isFromDate");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();

        c.set(year, month, day);
        mListener.onDatePicked(this, c, isFromDate);
    }

    // Override the Fragment.onAttach() method to instantiate the
    // NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the
            // host
            mListener = (DatePickerDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
}

这篇关于使用DialogFragment和Android注释设置日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆