片段!!我该如何将其用作片段

时间:2012-12-14 16:00:51

标签: android

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MyAndroidAppActivity extends Activity {

private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;

private int year;
private int month;
private int day;

static final int DATE_DIALOG_ID = 999;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setCurrentDateOnView();
    addListenerOnButton();

}

// display current date
public void setCurrentDateOnView() {

    tvDisplayDate = (TextView) findViewById(R.id.tvDate);
    dpResult = (DatePicker) findViewById(R.id.dpResult);

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    tvDisplayDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));

    // set current date into datepicker
    dpResult.init(year, month, day, null);

}

public void addListenerOnButton() {

    btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

    btnChangeDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(DATE_DIALOG_ID);

        }

    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       // set date picker as current date
       return new DatePickerDialog(this, datePickerListener, 
                     year, month,day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener 
            = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        tvDisplayDate.setText(new StringBuilder().append(month + 1)
           .append("-").append(day).append("-").append(year)
           .append(" "));

        // set selected date into datepicker also
        dpResult.init(year, month, day, null);

    }
};

}

有人可以帮助我如何使用碎片日期选择器代码

我试图让用户选择一个日期,计算出当前日期和他们选择的日期之间的差异,然后将日期差异与他们想要松散的磅数(重量)分开,以便消息显示为显示目标的空字符串。例如今天的日期是2012年12月14日,用户选择24/12/2012日期差异是10天,他们希望在10天内减掉10磅(重量)

我如何为它编程来计算差异/它的目标并显示一个结果显示每天1磅的目标

任何帮助都会很棒我真的失去了感谢!

1 个答案:

答案 0 :(得分:0)

你先扩展了Fragment类。 然后你覆盖onCreateView()来夸大你的内容视图。 您使用onActivityCreated绑定视图。