我試着按照android studio教程給datepicker。 這是我MainActivity.java如何在android中爲註冊表單提供輸入日期字段?
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看起來像
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");
}
}
當我嘗試運行代碼下面的錯誤我在cosole得到::: 錯誤:(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)
最後我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>
那裏我做了錯誤的?
感謝&問候
我已經實現並測試DatePickerFragment,看看[這裏] [1] [1]:HTTP ://stackoverflow.com/a/39031889/5398475 –