1

我在下面附上了我的代码。我想在编辑文本的触摸事件事件上获得日期选择器的对话。我没有收到任何错误,但也没有收到任何日期对话。

请帮助我哪里错了。提前感谢。

String TAG = "AdvancedSearchActivity";
EditText txtArrCity, txtDepCity, txtDate, txtTime;
public String year, month, day;
public static final int DATE_DIALOG_ID = 1;
// OnDateSetListener dateListener;
String DATE;

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.advance_search);

    txtArrCity = (EditText) findViewById(R.id.edittxt_ArrCity);
    txtDepCity = (EditText) findViewById(R.id.edittxt_DepCity);
    txtDate = (EditText) findViewById(R.id.edittxt_Date);
    txtTime = (EditText) findViewById(R.id.edittxt_Time);

    Calendar cal = Calendar.getInstance();
    year = String.valueOf(cal.get(Calendar.YEAR));
    month = String.valueOf(cal.get(Calendar.MONTH));
    day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));

    Log.i(TAG, "Year" + year);
    Log.i(TAG, "month" + (month + 1));
    Log.i(TAG, "Day" + day);
    updateDate();

    txtDate.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
            Log.i(TAG, "Inside On touch");
            return false;
        }
    });

}

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(TabSample.tabcontext,
                dateListener, Integer.parseInt(year),
                Integer.parseInt(month), Integer.parseInt(day));

    }
    return null;

}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int years, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        year = String.valueOf(years);
        month = String.valueOf(monthOfYear);
        day = String.valueOf(dayOfMonth);
        updateDate();
    }
};

private void updateDate() {

    month = String.valueOf((Integer.parseInt(month) + 1));
    if ((Integer.parseInt(month)) < 10) {
        Log.i(TAG, "@@@@@ month" + month);
        month = "0" + month;
        Log.i(TAG, "@@@@@ AFTER month" + month + " MONTH S" + month);
    }
    day = String.valueOf(day);
    if ((Integer.parseInt(day)) < 10) {
        day = "0" + day;
    }
    DATE = year + "-" + month + "-" + day;

    Log.i(TAG, "DATE : " + DATE);
    txtDate.setText(DATE);
}
4

2 回答 2

5

像这样试试

 public class name extends Activity {   
     private EditText dob;
        static final int DATE_DIALOG_ID = 0;
        @Override
            protected Dialog onCreateDialog(int id) {
                Calendar c = Calendar.getInstance();
                int cyear = c.get(Calendar.YEAR);
                int cmonth = c.get(Calendar.MONTH);
                int cday = c.get(Calendar.DAY_OF_MONTH);
                switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
                }
                return null;
            }
            private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
                // onDateSet method
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    String date_selected = String.valueOf(dayOfMonth)+" /"+String.valueOf(monthOfYear+1)+" /"+String.valueOf(year);
                    //Toast.makeText(getApplicationContext(), "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
                    dob.setText(date_selected);
                }
            };
        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.bankregform);
            dob=(EditText)findViewById(R.id.dob);
        /* For DOB EditText*/
                dob.setOnTouchListener(new OnTouchListener(){ 
                    public boolean onTouch(View v, MotionEvent event) { 
                        if(v == dob)
                            showDialog(DATE_DIALOG_ID);
                        return false;              
                    }
                });
        }
    }
于 2012-05-23T11:00:22.293 回答
0

试试这个...`activity_main.xml

<RelativeLayout 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"
tools:context=".MainActivity" >



<EditText
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="36dp"
    android:layout_marginTop="36dp"
    android:ems="10"
    android:inputType="date" />


 </RelativeLayout>

MainActivity.java

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends Activity {
static final int DATE_DIALOG_ID = 0;
private int mYear,mMonth,mDay;
EditText editText;

@SuppressWarnings("deprecation")
@SuppressLint("SimpleDateFormat")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Calendar c=Calendar.getInstance();
    mYear=c.get(Calendar.YEAR);
    mMonth=c.get(Calendar.MONTH);
    mDay=c.get(Calendar.DAY_OF_MONTH);
    //String dateFormat = "dd/MM/yyyy";
    editText = (EditText) findViewById(R.id.text);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    editText.setText( sdf.format(c.getTime()));

    editText.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);

        }
    });


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

        }

        return null;

    }


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

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;


editText.setText(new StringBuilder().append(mDay).append("/").append(mMonth+1).append("/").append(mYear));
        }

    };



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

`

于 2013-07-23T10:53:50.213 回答