![](/img/trans.png)
[英]How to hide FragmentTabHost android layout when soft keyboard appears?
[英]Android : Hide the soft keyboard when click the EditView
在我的應用程序中,我必須從用戶那里獲取日期。
首先,我使用一個按鈕,當用戶單擊該按鈕時,將出現一個對話框,其中包含日期選擇器並在一個TextView中獲取結果。
現在,我想用EditView替換按鈕。 在單擊EditView時,將必須出現對話框,並且不應出現鍵盤。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
EmployeeGlobal eg=(EmployeeGlobal)getApplicationContext();
String uid=eg.getMainUserId();
ArrayList<String>list=eg.getAllProjects(uid);
Spinner spinner = (Spinner)findViewById(R.id.all_projs);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
Toast.makeText(getApplicationContext(), "Report"+uid, Toast.LENGTH_LONG).show();
mStartDateDisplay=(TextView)findViewById(R.id.strdate);
mEndDateDisplay = (TextView) findViewById(R.id.enddate);
mEndDate = (Button) findViewById(R.id.end_dateb);
mStartDate=(Button)findViewById(R.id.start_dateb);
mYear=c1.get(Calendar.YEAR);
mMonth = c1.get(Calendar.MONTH);
mDay = c1.get(Calendar.DAY_OF_MONTH);
mStartDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_START);
//updateDisplay(mStartDateDisplay);
}
});
mEndDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_END);
//updateDisplay(mEndDateDisplay);
}
});
}
private void updateDisplay(TextView T) {
T.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mFromDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay(mStartDateDisplay);
}
};
private DatePickerDialog.OnDateSetListener mToDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay(mEndDateDisplay);
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_END:
return new DatePickerDialog(this,mToDateSetListener, mYear, mMonth, mDay);
case DATE_DIALOG_START:
return new DatePickerDialog(this,mFromDateSetListener,mYear, mMonth, mDay);
}
return null;
}
所以請有人幫我嗎?
我做的完全一樣,我正在發布一些代碼嘗試一下。 它應該工作..
EditText time_text_1;
time_text_1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//CREATE YOUR DATE PICKER DIALOG HERE.
}
return false;
}
});
在xml文件中為edittext聲明這些屬性。
android:clickable="true"
android:cursorVisible="false"
android:editable="false"
android:inputType="none"
android:singleLine="true"
希望它能工作。
如果不需要EditText,為什么不使用TextView。 並更改其背景,使其看起來像一個EditText。
我試圖在單擊EditBox時導航到另一個屏幕,發現它不會發生。 當我單擊時,它會彈出一個軟鍵盤。 再次按下時,導航到另一個屏幕。
我有同樣的問題,我所使用的是按鈕,但使用的背景類似於EditText。
因此,用戶會感覺自己是一個EditBox,但實際上是一個按鈕。
希望這些信息能對您有所幫助。
嘗試這兩行代碼
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mStartDate.getWindowToken(), 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.