java 如何在android中为注册表单提供输入日期字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39051210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to give input date field for registration form in android?
提问by Jaccs
I tried to give datepicker by following android studio tutorial. This is my MainActivity.java
我尝试按照 android studio 教程提供 datepicker。这是我的 MainActivity.java
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(),"datePicker");
}
}
DatePickerFragment.java looks like
DatePickerFragment.java 看起来像
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(),"datePicker");
}
}
When i try to run the code the following error i got in cosole ::: Error:(18, 20) error: no suitable method found for show(android.support.v4.app.FragmentManager,String) method DialogFragment.show(android.app.FragmentManager,String) is not applicable (argument mismatch; android.support.v4.app.FragmentManager cannot be converted to android.app.FragmentManager) method DialogFragment.show(FragmentTransaction,String) is not applicable (argument mismatch; android.support.v4.app.FragmentManager cannot be converted to FragmentTransaction)
当我尝试运行代码时,我在 cosole 中遇到以下错误 ::: Error:(18, 20) 错误:没有找到适合 show(android.support.v4.app.FragmentManager,String) 方法 DialogFragment.show( android.app.FragmentManager,String) 不适用(参数不匹配;android.support.v4.app.FragmentManager 无法转换为 android.app.FragmentManager)方法 DialogFragment.show(FragmentTransaction,String) 不适用(参数不匹配; android.support.v4.app.FragmentManager 不能转换为 FragmentTransaction)
Finally my activity_main.xml looks like
最后我的 activity_main.xml 看起来像
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.acknotech.kiran.dateexample.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
</LinearLayout>
where i made wrong?
我哪里做错了?
Thanks & Regards
感谢和问候
回答by Hasan shaikh
Add this in your layout file
将此添加到您的布局文件中
<LinearLayout
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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.geelani.datepicker.MainActivity">
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/pick_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick date"
android:onClick="showDatePickerDialog" />
and add this to your activity
并将其添加到您的活动中
public class MainActivity extends AppCompatActivity {
private int mYear,mMonth,mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button pickDate = (Button) findViewById(R.id.pick_date);
final TextView textView = (TextView) findViewById(R.id.date);
final Calendar myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// myCalendar.add(Calendar.DATE, 0);
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
textView.setText(sdf.format(myCalendar.getTime()));
}
};
pickDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
if (year < mYear)
view.updateDate(mYear,mMonth,mDay);
if (monthOfYear < mMonth && year == mYear)
view.updateDate(mYear,mMonth,mDay);
if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear,mMonth,mDay);
textView.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
dpd.show();
}
});
}
}
}
回答by sumandas
Try this, as picker has its own view that would pop up on using show and adding these values make it this as default date:
试试这个,因为选择器有它自己的视图,它会在使用 show 时弹出并添加这些值使其成为默认日期:
// On the click of registration field:
Calendar now = Calendar.getInstance()
new DatePickerDialog(classname.this, date, now
.get(Calendar.YEAR), now(Calendar.MONTH),
now(Calendar.DAY_OF_MONTH)).show(); // this shows the picker
回答by Chordin4tion
Here is an example to use DatePickerDialog:
以下是使用 DatePickerDialog 的示例:
final Calendar calendar = Calendar.getInstance();
dateFormat = new SimpleDateFormat("MM/dd/yyyy");
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar cal = Calendar.getInstance();
cal.set(year, monthOfYear, dayOfMonth);// Selected Date Goes Here.
// You Can Do Anything You Want Here
}
}, calendar.get(Calendar.YEAR)
, calendar.get(Calendar.MONTH)
, calendar.get(Calendar.DAY_OF_MONTH));