如何在编辑文本视图中显示当前日期

时间:2016-04-26 06:24:14

标签: android

这个我的代码

text.html.erb

错误: -

package com.clip.android;
/**
 * 
 * @author abhishek
 *
 */

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;


public class ClaimRegister extends ClaimActivity implements OnClickListener{

    EditText polid;
    /*DatePicker claimDate;*/
    String userValue;
    String getClaimDate;
    Spinner Spinner;
    String clmType;
    ImageButton calIcon;
    EditText claimDate;
    /*TextView b1;*/
    double a = 4.32;
    double b = Math.pow(10.0,8.0);
    int year,day,month;
    List<String> claimTypes=new ArrayList<String>();
/*    List<Customer> customers = new ArrayList<Customer>();*/


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_claim_register);
        setHeader("Register Your Claim", true, true);
        claimDate = (EditText)findViewById(R.id.editText1);
        polid = (EditText)findViewById(R.id.polid);
        /*claimDate = (DatePicker)findViewById(R.id.dp);*/
        Spinner=(Spinner) findViewById(R.id.spin);
        userValue = polid.getText().toString();
        calIcon = (ImageButton)findViewById(R.id.imageButton1); 
        calIcon.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Calendar c = Calendar.getInstance();

                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);
                /*claimDate.setMinDate((long)(c.getTimeInMillis()-(a*b)));
                claimDate.init(year, month, day, null);*/

                 claimDate.setText(String.valueOf(c.getTimeInMillis()-(a*b)));


                DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener()  {
                     public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
                        {   
                             Calendar cl = Calendar.getInstance();
                                cl.set(year, monthOfYear, dayOfMonth-1);
                                if(cl.before(Calendar.getInstance())){
                                    claimDate.setText(dayOfMonth+"-"+(monthOfYear)+"-"+year);
                                }else {
                                    claimDate.setText("");
                                    Toast.makeText(ClaimRegister.this, "Date Shold Be Within Current Date.",    Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                year = year; 
                                month = monthOfYear; 
                                day = dayOfMonth;

                        }
                }, year, month, day;
        ((Dialog) datePickerListener).show();

               }
            });
    AsyncCall drop = new AsyncCall();
    drop.execute();
    }

    private class AsyncCall extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... params) {
            Log.d("doBack", "0000");
            claimTypes = com.clip.webservice.GetAQuote.GetLineofBusiness("CLMTYPE", "getClmType");


            return null;
        }

        protected void onPostExecute(Void result) 
        {
            Log.d("onPost", "lol");
                    ArrayAdapter<String> adapter =  new ArrayAdapter<String>(ClaimRegister.this, android.R.layout.simple_spinner_dropdown_item,claimTypes);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    Log.d("onPost", "123");
                    Spinner.setAdapter(adapter);
                    Log.d("onPost", "hiiii");
                    Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {
                                String item = parent.getItemAtPosition(position).toString();
                                Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                                clmType= Spinner.getSelectedItem().toString();
                        }

                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub
                            String item=parent.getSelectedItem().toString();
                            Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                            clmType= Spinner.getSelectedItem().toString();
                        }

                    });

                     }
                    }


    public void submit(View view)
    {
        userValue = polid.getText().toString();

        if(userValue.trim().equals("")&& userValue != null){
            Toast.makeText(this, "Policy Id is required", Toast.LENGTH_LONG).show();
        }
        else{
        Intent intent = new Intent(this,ClaimRegisterPage.class);
        intent.putExtra("getclmType", clmType);
        intent.putExtra("userValue", userValue);
        Log.d("In submit", "userValue = "+userValue);
        startActivity(intent);
        }
    }
}

什么将当前日期设置为默认日期,并且我希望允许用户在当前日期之后15天不低于该日期。请帮助解决此问题。

4 个答案:

答案 0 :(得分:2)

DatePickerDialogDialog但是DatePickerDialog.OnDateSetListener不是可以施放的Dailog。因此,请展示DatePickerDialog,而不是显示datePickerListener。这是误差线

 ((Dialog) datePickerListener).show();

请参阅example如何创建和使用DatePickerDialog

答案 1 :(得分:0)

//Current date
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

// editText is the EditText  that should display it
editText.setText(currentDateTimeString);

对于15天的限制,您可以在设置编辑文本的日期之前使用if else条件。如果用户在15天之前选择日期,则编辑文本保持为空

答案 2 :(得分:0)

我在代码中注意到了几个错误, 1)替换

calIcon.setOnClickListener(new Button.OnClickListener()

calIcon.setOnClickListener(new View.OnClickListener() 

2)在类型转换下面导致崩溃

 ((Dialog) datePickerListener).show();

您应该创建一个Dialog实例,使用require属性构建它并设置监听器。然后调用show()方法。请参阅Datepicker: How to popup datepicker when click on button and store value in variable

答案 3 :(得分:0)

已解决的代码

package com.clip.android;
/**
 * 
 * @author abhishek
 *
 */

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;


public class ClaimRegister extends ClaimActivity implements OnClickListener{

    EditText polid;
    String userValue;
    String getClaimDate;
    Spinner Spinner;
    String clmType;
    ImageButton calIcon;
    EditText claimDate;
    double a = 1.296;
    double b = Math.pow(10.0,9.0);

    static final int DATE_PICKER_ID = 0; 
    int year,day,month;
    List<String> claimTypes=new ArrayList<String>();



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_claim_register);
        setHeader("Register Your Claim", true, true);
        claimDate = (EditText)findViewById(R.id.editText1);
        polid = (EditText)findViewById(R.id.polid);
        Spinner=(Spinner) findViewById(R.id.spin);
        userValue = polid.getText().toString();
        calIcon = (ImageButton)findViewById(R.id.imageButton1); 
        claimDate.setEnabled(false);

        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);


        claimDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(day).append("-").append(month + 1).append("-")
        .append(year).append(" "));
        calIcon.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            public void onClick(View v) {

                // On button click show datepicker dialog 


                showDialog(DATE_PICKER_ID);
            }

        });


    AsyncCall drop = new AsyncCall();
    drop.execute();
    }

    private class AsyncCall extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... params) {
            Log.d("doBack", "0000");
            claimTypes = com.clip.webservice.GetAQuote.GetLineofBusiness("CLMTYPE", "getClmType");


            return null;
        }

        protected void onPostExecute(Void result) 
        {
            Log.d("onPost", "lol");
                    ArrayAdapter<String> adapter =  new ArrayAdapter<String>(ClaimRegister.this, android.R.layout.simple_spinner_dropdown_item,claimTypes);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    Log.d("onPost", "123");
                    Spinner.setAdapter(adapter);
                    Log.d("onPost", "hiiii");
                    Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {
                                String item = parent.getItemAtPosition(position).toString();
                                Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                                clmType= Spinner.getSelectedItem().toString();
                        }

                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub
                            String item=parent.getSelectedItem().toString();
                            Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                            clmType= Spinner.getSelectedItem().toString();
                        }

                    });

                     }
                    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_ID:

            // open datepicker dialog. 
            // set date picker for current date 
            // add pickerListener listner to date picker
            //disabled the dates lesser than 15 days from current date onload of date picker dialog 
            DatePickerDialog dp = new DatePickerDialog(this, pickerListener, year, month,day);
                Calendar c = Calendar.getInstance();
                dp.getDatePicker().setMinDate(((long)(c.getTimeInMillis()-(a*b))));
                return dp;
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = 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;

            // Show selected date 

            claimDate.setText(new StringBuilder().append(day)
                    .append("-").append(month + 1).append("-").append(year)
                    .append(" "));

           }
        };


    public void submit(View view)
    {
        userValue = polid.getText().toString();
            //Validation for Policy Id since it's mandatory
        if(userValue.trim().equals("")&& userValue != null){
            Toast.makeText(this, "Policy Id is required", Toast.LENGTH_LONG).show();
        }
        else{
        Intent intent = new Intent(this,ClaimRegisterPage.class);
        intent.putExtra("getclmType", clmType);
        intent.putExtra("userValue", userValue);
        Log.d("In submit", "userValue = "+userValue);
        startActivity(intent);
        }
    }
}