2017-06-27 20 views
0

你好,我已經在android中給日期和時間驗證,但是當我不選擇日期時,它會顯示錯誤消息,但是從datepicker中選擇日期後,它再次顯示錯誤如何解決這裏是我的代碼編輯文本的日期和時間驗證錯誤在android中

public class ProfileDetail extends AppCompatActivity implements DatePickerDialog.OnDateSetListener { 
    Toolbar toolbar; 
    String description, str_requirement, str_date, str_time, str_noofperson, str_address; 
    TextView txt_description; 
    EditText et_requirment, date_pick, time_set, et_no_of_person, et_address_service; 
    Calendar calendar; 
    DatePickerDialog datePickerDialog; 
    int Year, Month, Day; 
    Button btn_submit; 

    @RequiresApi(api = Build.VERSION_CODES.N) 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.profile_details); 
     Bundle extras = getIntent().getExtras(); 
     description = extras.getString("description"); 
     txt_description = (TextView) findViewById(R.id.txtservicedetails); 
     toolbar = (Toolbar) findViewById(R.id.toolbarforprofile); 
     setSupportActionBar(toolbar); 
     et_requirment = (EditText) findViewById(R.id.editText_requirement); 
     et_no_of_person = (EditText) findViewById(R.id.et_noofperson); 
     et_address_service = (EditText) findViewById(R.id.et_serviceaddress); 
     date_pick = (EditText) findViewById(R.id.et_dateof_visit); 
     time_set = (EditText) findViewById(R.id.et_time); 

     btn_submit = (Button) findViewById(R.id.btn_submit); 
     calendar = Calendar.getInstance(); 
     Year = calendar.get(Calendar.YEAR); 
     Month = calendar.get(Calendar.MONTH); 
     Day = calendar.get(Calendar.DAY_OF_MONTH); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     toolbar.setTitle("Need-Hlp"); 
     txt_description.setText(description); 
     //date_pick.setInputType(InputType.TYPE_NULL); 
     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intentfromprevious = new Intent(ProfileDetail.this, NavigationDrawer.class); 
       intentfromprevious.putExtra("description", description); 
       intentfromprevious.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       intentfromprevious.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
       intentfromprevious.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       startActivity(intentfromprevious); 

      } 
     }); 



     date_pick.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.N) 
      @Override 
      public void onClick(View v) { 

       datePickerDialog = DatePickerDialog.newInstance(ProfileDetail.this, Year, Month, Day); 

       datePickerDialog.setThemeDark(false); 

       datePickerDialog.showYearPickerFirst(false); 

       datePickerDialog.setAccentColor(Color.parseColor("#009688")); 

       datePickerDialog.setTitle("Select Date From DatePickerDialog"); 

       datePickerDialog.show(getFragmentManager(), "DatePickerDialog"); 


      } 
     }); 
     time_set.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.N) 
      @Override 
      public void onClick(View v) { 
       Calendar mcurrentTime = Calendar.getInstance(); 
       int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
       int minute = mcurrentTime.get(Calendar.MINUTE); 

       TimePickerDialog mTimePicker; 
       mTimePicker = new TimePickerDialog(ProfileDetail.this, new TimePickerDialog.OnTimeSetListener() { 
        @Override 
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
         time_set.setText(selectedHour + ":" + selectedMinute); 
        } 
       }, hour, minute, true); 
       mTimePicker.setTitle("Select Time"); 
       mTimePicker.show(); 

      } 
     }); 


     //////Submit button onclicklistener 
     btn_submit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (et_requirment.getText().toString().equals("")) { 
        et_requirment.setError("Requirement cannot be blank"); 
        et_requirment.requestFocus(); 
        return; 
       } 
       if (date_pick.getText().toString().equals("")) { 
        date_pick.setError("Date cannot be blank"); 

        //date_pick.requestFocus(); 
        return; 
       } 

       if (time_set.getText().toString().equals("")) { 
        time_set.setError("Time cannot be blank"); 
        //edittext_phone.requestFocus(); 
        return; 
       } 

       if (et_no_of_person.getText().toString().toString().equals("")) { 
        et_no_of_person.setError("No of person cannot be blank"); 
        //edittext_phone.requestFocus(); 
        return; 
       } 
       if (et_address_service.getText().toString().equals("")) { 
        et_address_service.setError("Address cannot be blank"); 
        //edittext_Password.requestFocus(); 
        return; 
       } 


       if (!et_requirment.getText().toString().trim().equals("") && !date_pick.getText().toString().equals("") && 
         time_set.getText().toString().trim().equals("") && 
         et_no_of_person.getText().toString().trim().equals("") && 
         !et_address_service.getText().toString().equals("")) { 
        Toast.makeText(ProfileDetail.this, "You Request Submitted", Toast.LENGTH_SHORT).show(); 
        Intent intentforlist = new Intent(ProfileDetail.this, NavigationDrawer.class); 
        intentforlist.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        intentforlist.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
        intentforlist.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
        startActivity(intentforlist); 
       } 

      } 
     }); 
    } 

Here Is MY XML: 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="-45.5dp" 
        android:layout_marginRight="16dp" 
        android:layout_marginTop="32dp"> 


        <LinearLayout 
         android:id="@+id/profile_details" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:background="@color/demo" 

          android:orientation="vertical" 
          android:padding="16dp"> 

          <TextView 
           android:id="@+id/txtservicedetails" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:layout_marginBottom="20dp" 
           android:layout_marginTop="40dp" 
           android:gravity="center" 
           android:text="@string/app_name" 
           android:textColor="#fff" 
           android:textSize="20sp" 
           android:textStyle="bold" /> 

          <EditText 
           android:id="@+id/editText_requirement" 
           android:layout_width="match_parent" 
           android:layout_height="70dp" 
           android:layout_margin="3dp" 
           android:background="#fff" 
           android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" 
           android:gravity="top" 
           android:hint="Your Requirement" 
           android:inputType="textMultiLine|textCapSentences" 
           android:padding="6dp" 
           android:textColor="#000" /> 

          <EditText 
           android:id="@+id/et_dateof_visit" 
           android:layout_width="match_parent" 
           android:layout_height="40dp" 
           android:layout_margin="5dp" 
           android:background="#fff" 

           android:hint="Date Of Visit" 
           android:inputType="date" 
           android:maxLines="1" 
           android:padding="5dp" 
           android:textColor="#000" /> 

          <EditText 
           android:id="@+id/et_time" 
           android:layout_width="match_parent" 
           android:layout_height="40dp" 
           android:layout_margin="5dp" 
           android:background="#fff" 

           android:hint="Time Of Visit" 
           android:inputType="time" 
           android:maxLines="1" 
           android:padding="5dp" 
           android:textColor="#000" /> 

          <EditText 

           android:id="@+id/et_noofperson" 
           android:layout_width="match_parent" 
           android:layout_height="40dp" 
           android:layout_margin="5dp" 
           android:background="#fff" 
           android:focusable="true" 
           android:hint="No Of Person You Need" 
           android:inputType="number" 
           android:maxLength="2" 
           android:maxLines="1" 
           android:padding="5dp" 
           android:singleLine="true" 
           android:text="" /> 

          <EditText 

           android:id="@+id/et_serviceaddress" 
           android:layout_width="match_parent" 
           android:layout_height="70dp" 
           android:layout_margin="5dp" 
           android:background="#fff" 
           android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890/-,:" 
           android:focusable="true" 
           android:gravity="top" 
           android:hint="Service Address" 
           android:inputType="textMultiLine|textCapSentences" 
           android:maxLines="4" 
           android:padding="10dp" 
           android:singleLine="true" 
           android:text="" /> 


         </LinearLayout> 

         <RelativeLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center" 
          android:layout_marginBottom="20dp" 
          android:layout_marginTop="-20dp" 
          android:gravity="center"> 

          <Button 
           android:id="@+id/btn_submit" 
           style="@style/Widget.AppCompat.Button.Colored" 
           android:layout_width="wrap_content" 
           android:layout_height="40dp" 
           android:background="@drawable/rounded_green" 
           android:text="Submit" 
           android:textStyle="bold" /> 
+0

你的按鈕收到錯誤的作品點擊? @dipaks –

+0

是按鈕點擊@ Dinesh – dipaks

回答

0

添加此您日期點擊希望它會爲你

final Calendar myCalendar = Calendar.getInstance(); 
      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); 
        String myFormat = "yyyy-MM-dd"; // your format 
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault()); 

        date_pick.setText(sdf.format(myCalendar.getTime())); 
       } 

      }; 
      new DatePickerDialog(this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();