Android DatePicker和Button问题

时间:2014-08-14 16:32:41

标签: android button android-fragments datepicker

我正在尝试在用户点击“设置你的生日”按钮时显示一个DatePicker对话框,然后我需要的是按钮然后在按钮中显示用户的选择。我已尝试使用TextView在本网站上列出的一些解决方案,但都无济于事。 DatePicker显示得很好,但我无法将数据显示在Button上。我尝试过这些解决方案:DatePicker not updating Textview in Android Display datepickers value in a textview Android: DatePicker and DatePicker Dialog How to transfer the formatted date string from my DatePickerFragment?

这是我的代码:

首先是DatePickerFragment:

public class DatePickerFragment extends DialogFragment实现DatePickerDialog.OnDateSetListener {

TheListener listener;

public interface TheListener {
    public void returnDate(String date);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

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

    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(year, monthOfYear, dayOfMonth);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate = sdf.format(c.getTime());

    if (listener != null) {
        listener.returnDate(formattedDate);
    }
}

}

现在,正在调用它的片段:

public class SignUpFragment extends Fragment实现MyBirthday.TheListener {

private Button btnBirthday;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_signup, container, false);

    return view;
}

@Override
public void onResume() {
    super.onResume();

    btnBirthday = (Button) getActivity().findViewById(R.id.signup_birthday_button);
    btnBirthday.setText("Set your birthday");
    btnBirthday.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment picker = new DatePickerFragment();
            picker.show(getFragmentManager(), "datePicker");
        }
    });
}

@Override
public void returnDate(String date) {
    btnBirthday.setText(date);
}

}

非常感谢任何和所有帮助。如果这是一个重复的问题,我很抱歉,但我找不到让我的程序按照我希望的方式行事的方法。我在这里找不到解决方案。

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个。这将向您展示如何在textview中呈现信息,但您也可以将其传输到按钮。您可以在http://www.ahotbrew.com/android-datepicker-example/

上找到完整的源代码
package com.ahotbrew.datepicker; 
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView; 
import java.util.Calendar;

public class MainActivity extends ActionBarActivity {

    public static TextView SelectedDateView;

    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) {
            SelectedDateView.setText("Selected Date: " + (month + 1) + "-" + day + "-" + year);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SelectedDateView = (TextView) findViewById(R.id.selected_date);
    }

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

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