android datepickerfragment将信息传递给主要活动

时间:2016-03-12 18:01:24

标签: android date datepicker transfer implements

我是编程和android的新手。我遇到了datePickerfragment的问题。在mainActivity上实现DatePickerFragment.OnFragmentInteractionListener时出错,它表示" 类MainActivity必须声明为abstract或实现抽象方法" returnDate(String)' in' OnFragmentInteractionListener' 。"

我使用了http://developer.android.com/guide/topics/ui/controls/pickers.html中显示的datePicker。

datePicker有效,但我想将所选值传输到mainActivity,我在其他帖子中提到了一些提示:How to transfer the formatted date string from my DatePickerFragment?

我真的不知道自己错过了什么。我还想知道是否可以传输de Date值而不是String值。我想在mainActivity中操作Date。希望可以有人帮帮我。这是我的代码:

package com.example.android.datepickertest;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity
        implements DatePickerFragment.OnFragmentInteractionListener
        {
 private TextView dataSelecionada;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dataSelecionada = (TextView)findViewById(R.id.data_selecionada);
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");

    }

@Override
    public void setTextDate (int year, int month, int day) {
        TextView displayDataSelecionada = (TextView) findViewById(R.id.data_selecionada);
        dataSelecionada.setText(day + "/" + month +"/" + year);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

和datePickerFragment:n

package com.example.android.datepickertest;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;

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


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link DatePickerFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link DatePickerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    OnFragmentInteractionListener listener;

    public interface OnFragmentInteractionListener {
        public void returnDate(String date);

    };

    @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);
        listener=(OnFragmentInteractionListener)getActivity();

        // 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) {
        // Do something with the date chosen by the user
        Calendar c= Calendar.getInstance();
        c.set(year,month,day);
        SimpleDateFormat date = new SimpleDateFormat();
        String dateSet = date.format(c.getTime());
        if (listener !=null){
            listener.returnDate(dateSet);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

错误有答案,当您实现接口时,您应该实现接口中的所有方法。

在你的情况下,你的主要活动中没有方法返回。

1.创建方法returnDate或者如果你使用Android工作室,只需转到文件MainActivity并将光标放在公共类MainActivity'然后按Alt + Enter,将自动创建方法,并通过此方法在主活动中输入日期。

  1. 如果您需要日期而不是字符串,请将returnndate方法的参数更改为Date,并删除该Simpledateformat而不是
  2. 新日期(c.getTime())