gpt4 book ai didi

android - 日期选择器没有运行?

转载 作者:行者123 更新时间:2023-11-30 03:53:35 27 4
gpt4 key购买 nike

如果我运行它,不幸的是该应用程序已关闭。但我试了很多,我无法在文本字段中获取日期。显示对话框中有错误,它返回添加 super 警告。 Layout image

package com.task.reminder;

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

@SuppressWarnings("unused")
public class Add extends Activity
{

static final int DATE_ID = 0;
private int tMonth;
private int tYear;
private int tDay;
private EditText et2 =(EditText)findViewById(R.id.editText2);
private Button datebut=(Button)findViewById(R.id.button3);
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);

getdate();
}



private void getdate()
{
datebut.setOnClickListener(new View.OnClickListener() {


@SuppressWarnings("deprecation")
@Override
public void onClick(View v)
{
showDialog(DATE_ID);
}
});

final Calendar c = Calendar.getInstance();
tYear = c.get(Calendar.YEAR);
tMonth = c.get(Calendar.MONTH);
tDay = c.get(Calendar.DAY_OF_MONTH);
displaydate();

}

protected Dialog onCreateDialog(int id)
{
switch(id)
{
case DATE_ID:
return new DatePickerDialog(this, datePickerListener,
tYear, tMonth,tDay);
}
return null;
}


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

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth)
{
tYear=year;
tMonth=monthOfYear;
tDay=dayOfMonth;
displaydate();
}
};


private void displaydate()
{


et2.setText(new StringBuilder().append(tMonth+1).append("-").append(tDay).append("-").append(tYear).append(" "));


}


}

我需要一些帮助,感谢您的贡献。

最佳答案

试试下面的例子吧

日历 Activity .java

public class CalendarActivity extends Activity
{
private EditText Calctrl;
static final int DATE_DIALOG_ID = 0;
final Calendar c = Calendar.getInstance();
private int mYear, mMonth, mDay;
private String sdate;
private String[] arrayMonth = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calctrl = (EditText) findViewById(R.id.datepicker);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
sdate = currentDate(mYear, mMonth, mDay);
Calctrl.setText(sdate);

Calctrl.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
showDialog(DATE_DIALOG_ID);
return true;
}
});
}

@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);
}
return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
switch (id)
{
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
{
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String sdate = currentDate(mYear, mMonth, mDay);
Calctrl.setText(sdate);
}
};

private static String LPad(String schar, String spad, int len)
{
String sret = schar;
for (int i = sret.length(); i < len; i++)
{
sret = spad + sret;
}
return new String(sret);
}

private String currentDate(int year, int month, int day)
{
String sdate = arrayMonth[month] + " " + LPad(day + "", "0", 2) + ", "+ year;
return sdate;
}
}

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_background"
android:orientation="vertical" >

<EditText
android:id="@+id/datepicker"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:editable="false"
android:inputType="none" />

</LinearLayout>

来源here

关于android - 日期选择器没有运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701166/

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