2015-03-31 162 views
0

我想說明一個datepicker時EditText得到focus如何讓EditText獲取焦點時不顯示虛擬鍵盤?

public class DatePickerUtil { 

    private Context ctxt; 
    private Calendar calendrier = Calendar.getInstance(); 
    private EditText chp; 
    private KeyListener keyListener; 
    private OnTouchListener touchListener; 
    private boolean canceled; 
    private DatePickerDialog picker; 

    public DatePickerUtil(Context context, EditText field) { 

     ctxt = context; 
     chp = field; 

     keyListener = new KeyListener() { 

      @Override 
      public int getInputType() { 
       // TODO Auto-generated method stub 
       return 0; 
      } 

      @Override 
      public boolean onKeyDown(View view, Editable text, int keyCode, 
        KeyEvent event) { 
       // TODO Auto-generated method stub 
       return false; 
      } 

      @Override 
      public boolean onKeyUp(View view, Editable text, int keyCode, 
        KeyEvent event) { 
       // TODO Auto-generated method stub 
       return false; 
      } 

      @Override 
      public boolean onKeyOther(View view, Editable text, KeyEvent event) { 
       // TODO Auto-generated method stub 
       return false; 
      } 

      @Override 
      public void clearMetaKeyState(View view, Editable content, 
        int states) { 
       // TODO Auto-generated method stub 

      } 

     }; 

     touchListener = new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_UP) { 
        canceled = false; 
        show(); 
       } 
       return false; 
      } 

     }; 
    } 

    private void show() { 
     OnDateSetListener evt = new OnDateSetListener() { 
      @Override 
      public void onDateSet(DatePicker view, int annee, int mois, int jour) { 
       if (!canceled) { 
        calendrier.set(annee, mois, jour); 
        chp.setText(DateFormat.format("yyyy-MM-dd", calendrier)); 
       } 
      } 
     }; 
     picker = new DatePickerDialog(ctxt, evt, calendrier.get(Calendar.YEAR), 
                 calendrier.get(Calendar.MONTH), 
                 calendrier.get(Calendar.DAY_OF_MONTH)); 
     picker.setButton(DialogInterface.BUTTON_NEGATIVE, ctxt.getResources().getString(R.string.button_cancel), new OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        canceled = true; 
        dialog.dismiss(); 
       } 
      }); 
     picker.show(); 
    } 

    public void attache() { 
     chp.setOnTouchListener(touchListener); 
     chp.setKeyListener(keyListener); 
    } 
} 

在運行時,焦點不是EditText裏面只是圍繞現場:

enter image description here

正如你可以看到用戶無法編輯EditText的內容,所以virtualkeyboard是沒有必要的。問題是當EditText得到focus時顯示virtualkeyboard!那麼如何在EditText得到focus時不顯示?

+0

你有沒有試着用'安卓的inputType = 「日期」'在XML? – NSimon 2015-03-31 09:13:37

+0

是的,但Android的默認格式爲「DD/MM/YYYY」,儘管我的格式爲「YYYY-MM-DD」 – pheromix 2015-03-31 09:24:51

回答

1

確定使用這種方法

public void hideSoftKeyboard(Activity activity) { 
     InputMethodManager inputMethodManager = (InputMethodManager) activity 
       .getSystemService(Activity.INPUT_METHOD_SERVICE); 
     if (activity.getCurrentFocus() != null) 
      inputMethodManager.hideSoftInputFromWindow(activity 
        .getCurrentFocus().getWindowToken(), 0); 
    } 
+0

你可以把這個方法放在一個類中,把它改成靜態的,並且可以從任何需要隱藏屏幕的地方調用 – 2015-03-31 12:49:18

0

嘗試這個編程

YourEditText=(EditText) findViewById(R.id.editText1); 
YourEditText.setInputType(InputType.TYPE_NULL); 
+0

不,它不起作用! – pheromix 2015-03-31 09:39:22

0

Bascically你可以使用像翻譯任何日期:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
String date = format.format(Date.parse("Your date string")); 

,那麼你可以檢測當用戶選擇一個日期,並顯示你想要的版本與...合作。

在這裏看到更多:http://developer.android.com/reference/java/text/SimpleDateFormat.html

+0

是的,但它修復虛擬鍵盤問題? – pheromix 2015-03-31 09:33:29

+0

那麼它不會真的刪除鍵盤,但用戶將能夠輸入一個日期,你可以將其轉換爲任何你想要的格式 – NSimon 2015-03-31 09:48:49

0

將您EDITTEXT到的TextView和添加的onclick偵聽器視圖,並顯示出相同的像下面的日期選擇器對話框:

private DatePickerDialog mDatePicker; 
    static final int DATE_PICKER_ID = 1111; 
private int mYear, mMonth, mDay, mHour, mMin; 
    mTvDate.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 
       showDialog(DATE_PICKER_ID); 

      } 
     }); 


@Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_PICKER_ID: 
      mDatePicker = new DatePickerDialog(this, pickerListener, mYear, 
        mMonth, mDay); 
      mDatePicker.setTitle("Select Date"); 
      return mDatePicker; 

     } 
     return null; 
    } 

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

     // when dialog box is closed, below method will be called. 
     @SuppressLint("SimpleDateFormat") 
     @Override 
     public void onDateSet(DatePicker view, int selectedYear, 
       int selectedMonth, int selectedDay) { 
      mDay = selectedDay; 
      mMonth = selectedMonth; 
      mYear = selectedYear; 
      Calendar c = Calendar.getInstance(); 
      c.set(mYear, mMonth, mDay); 
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
      String selectedDate = df.format(c.getTime()); 
      mTvDate.setText(selectedDate); 
     } 
    }; 
0

試試這個裏面的EditText onclicklistener方法。這將強制鍵盤隱藏。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
+0

它不起作用! – pheromix 2015-03-31 11:12:25