2016-08-20 56 views
1

我試着按照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> 

那裏我做了錯誤的?

感謝&問候

+0

我已經實現並測試DatePickerFragment,看看[這裏] [1] [1]:HTTP ://stackoverflow.com/a/39031889/5398475 –

回答

0

試試這個,因爲選擇器都有自己的觀點,即會彈出利用表演和將這些值使其設置爲默認日期:

// 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 
0

下面是一個例子,使用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)); 
1

在佈局文件中添加此

<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" /> 

,並添加到您的活動

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(); 

     } 
    }); 

} 

}

+0

非常感謝您的關注。剛纔我的工作與http://stackoverflow.com/a/39031889/5398475完成這個... – Jaccs

+0

如果它爲你工作..然後你可以接受我的答案,並投票。 –

相關問題