gpt4 book ai didi

android datepickerfragment 将信息传递给主要 Activity

转载 作者:行者123 更新时间:2023-11-30 01:29:33 24 4
gpt4 key购买 nike

我是编程和 Android 方面的新手。我在使用 datePickerfragment 时遇到问题。在 mainActivity 上实现 DatePickerFragment.OnFragmentInteractionListener 时出错,它说“MainActivity 类必须声明为抽象或在‘OnFragmentInteractionListener’中实现抽象方法‘returnDate(String)’。”

我使用了 datePicker,如 http://developer.android.com/guide/topics/ui/controls/pickers.html 所示.

datePicker 可以工作,但我想将选择的值传输到 mainActivity,我在另一篇文章中得到了一些提示:How to transfer the formatted date string from my DatePickerFragment? .

我真的不知道我错过了什么。我还想知道是否可以传输日期值而不是字符串值。我想在 mainActivity 中操作日期。希望可以有人帮帮我。这是我的代码:

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

}
}

最佳答案

错误有答案,当你实现一个接口(interface)时,你应该实现接口(interface)中的所有方法。

在您的情况下,您的 mainactivity 中没有方法 returndate。

1.创建方法 returnDate 或者如果您使用 Android studio,只需转到文件 MainActivity 并将光标放在“public class MainActivity”上,然后按 Alt+Enter,系统将自动创建该方法,您将在其中输入日期你的主要 Activity 通过这个方法。

  1. 如果您需要日期而不是字符串,请将 returndate 方法的参数更改为 Date,并删除 Simpledateformat 而是使用

新日期(c.getTime())

关于android datepickerfragment 将信息传递给主要 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com