1

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

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

下面的代码:

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

应该实现日期写入的函数是:

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

它应该将日期写入 dateFromTV

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

它应该将日期写入 dateToTV

我解决了这个问题。

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

4

1 回答 1

3

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

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

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

}

活动主.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:

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");
        }
    }
}
于 2013-06-05T15:47:18.107 回答