1

これを実行すると、残念ながらアプリが閉じられます。しかし、私はたくさん試しましたが、テキストフィールドに日付を取得できません。showdialog にエラーがあります。スーパーワーニングを追加して返します。 レイアウトイメージ

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


        }    


}

あなたの貢献に感謝します。

4

3 に答える 3

1

動作する次の例を試してください

カレンダー アクティビティ .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;
 }
}

main.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>

ソースはこちら

于 2012-12-04T10:57:17.557 に答える
0

アクティビティのレイアウトを設定せずにビューを見つけようとしているので、アクティビティのメソッドの後にすべてのfindViewById行があります。コードを次のように変更します。setContentView(R.layout.activity_add);onCreate(Bundle savedInstanceState)

public class Add extends Activity 
{

    static final int DATE_ID = 0;
    private  int tMonth;
    private int tYear;
    private int tDay;
    EditText et2 ;
    Button datebut;
    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        et2 =(EditText)findViewById(R.id.editText2);
        datebut=(Button)findViewById(R.id.button3);
          // your code here....
于 2012-12-04T10:53:42.417 に答える
0

以下は、ボタンをクリックしたときにテキストビューに日付を表​​示するために行った私のコードです。..ボタンをクリックすると、日付ピッカーがポップアップします。日付を選択するだけで、その日付がそれぞれのテキストビューに設定されます。これが役立つことを願っています..

TextView starttext1;
Button startdatecustom;
DateFormat fmtDateAndTime = DateFormat.getDateInstance();

private int year;
private int month;
private int day;
Calendar myCalendar = Calendar.getInstance();
static final int DATE_DIALOG_ID = 999;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
starttext1 = (TextView) findViewById(R.id.startdateedittext1);

startdatecustom = (Button) findViewById(R.id.btnstdateactcutom);

startdatecustom.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {



            new DatePickerDialog(MainActivity.this, d, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show(); 

        }
    });

}

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }
};

private void updateLabel() {
    starttext1.setText(fmtDateAndTime.format(myCalendar.getTime()));
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        // set date picker as current date
        return new DatePickerDialog(this, datePickerListener, year, month,
                day);
    }
    return null;
}

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

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

    }
};
于 2012-12-05T11:54:57.887 に答える