【问题标题】:Date Was Not Changed When I Pick a New Date in Android using DatePicker当我使用 DatePicker 在 Android 中选择新日期时,日期未更改
【发布时间】:2015-12-15 12:09:22
【问题描述】:

在我的程序中,我想在获取当前日期后从用户那里获取当前日期文本视图想要在 100 天后自动显示所选日期。

当我第一次运行我的应用程序时,我选择 15-12-2015 文本视图显示 24-03-2016 自动。

当我将日期更改为接下来的 3 天时,即选择 18-12-2015 文本视图显示相同的日期(24-03-2016

这是我的代码: 活动.xml

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@id/textView"
    android:editable="false"
   android:hint="Select the Date "
    />

<Button
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_below="@+id/textView"
    android:layout_toRightOf="@+id/editText"
    android:background="@drawable/calender"
    android:layout_alignBottom="@+id/editText"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:id="@+id/button"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView3"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />

MainActivity.java

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    private Button ib;
    private Calendar cal;
    private int day;
    private int month;
    private int year;
    private EditText et;
    private TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib = (Button) findViewById(R.id.button);
        cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
        et = (EditText) findViewById(R.id.editText);
        display = (TextView) findViewById(R.id.textView3);
        ib.setOnClickListener((View.OnClickListener) this);

    }
@Override
    public void onClick(View v) {
        showDialog(0);
    }
    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        return new DatePickerDialog(this, datePickerListener, year, month, day);
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
             et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                    + selectedYear);
 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Calendar c = Calendar.getInstance();
           // c.setTime(dtStartDate);
            c.add(Calendar.DATE, 100);  // number of days to add
            String dt = sdf.format(c.getTime());  // dt is now the new date
            display.setText(dt);
        }
    };

【问题讨论】:

    标签: java android date android-datepicker


    【解决方案1】:

    试试这个:-

    activity.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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="70dp"
            android:onClick="setDate"
            android:text="@string/date_button_set" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="24dp"
            android:text="@string/date_label_set"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_marginTop="66dp"
            android:layout_toLeftOf="@+id/button1"
            android:text="@string/date_view_set"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="72dp"
            android:text="@string/date_selected"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </RelativeLayout>
    

    mainactivity.java :-

    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.support.v4.app.DialogFragment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.Calendar;
    
    public class MainActivity extends AppCompatActivity {
        private DatePicker datePicker;
        private Calendar calendar;
        private TextView dateView;
        private int year, month, day;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            dateView = (TextView) findViewById(R.id.textView3);
            calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
    
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
            showDate(year, month + 1, day);
        }
        @SuppressWarnings("deprecation")
        public void setDate(View view) {
            showDialog(999);
            Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
                    .show();
        }
    
        @Override
        protected Dialog onCreateDialog(int id) {
            // TODO Auto-generated method stub
            if (id == 999) {
                return new DatePickerDialog(this, myDateListener, year, month, day);
            }
            return null;
        }
    
        private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                // arg1 = year
                // arg2 = month
                // arg3 = day
                showDate(arg1, arg2+1, arg3);
            }
        };
    
        private void showDate(int year, int month, int day) {
            dateView.setText(new StringBuilder().append(day).append("/")
                    .append(month).append("/").append(year));
        }
    
    
    
    }
    

    【讨论】:

    • 我想用当前选择的日期添加 100 天 @NikPatel
    【解决方案2】:

    您没有使用日期选择器提供的日期。

    试试这个:

        public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
                 et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                        + selectedYear);
     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Calendar c = Calendar.getInstance();
                c.set(selectedYear, selectedMonth, selectedDay); //set the date here
               // c.setTime(dtStartDate);
                c.add(Calendar.DATE, 100);  // number of days to add
                String dt = sdf.format(c.getTime());  // dt is now the new date
                display.setText(dt);
            }
    

    更新: 将“selectedMonth+1”更改为“selectedMonth”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      相关资源
      最近更新 更多