我有一个带有Spinner和EditText的活动。如果我关注EditText,则会显示键盘。好。如果我然后触摸Spinner,键盘应该被隐藏。我得到了这个(见代码)。
现在我的问题: 当我关闭Spinner时(无论是否选择了某个项目),如果键盘再次为EditText打开而不点击它,那将会很不错。 (当打开Spinner时,焦点仍然在EditText上)
EditText myET = (EditText) findViewById(R.id.editText);
Spinner mySpin = (Spinner) findViewById(R.id.spinner);
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
Spinner.OnTouchListener hideKeyboard = new Spinner.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return false;
}
};
mySpin.setOnTouchListener(hideKeyboard);
答案 0 :(得分:1)
在onItemSelected
中使用此代码InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
这将自动打开下一个editText的键盘。
答案 1 :(得分:1)
使用此代码在所选项目上再次显示键盘:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// open keyboard code here.
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
注意:使用点击监听器来满足您的要求。因为spinner上的直接点击监听器不受支持,所以让一个类扩展微调器并在click方法上过度骑行,并且在这个方法中做你想做的事。
/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value */
public class CutomSpinner extends Spinner {
public CutomSpinner(Context context)
{ super(context); }
public CutomSpinner (Context context, AttributeSet attrs)
{ super(context, attrs); }
public CutomSpinner (Context context, AttributeSet attrs, int defStyle)
{ super(context, attrs, defStyle); }
@Override public void
setSelection(int position, boolean animate)
{
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@Override public void
setSelection(int position)
{
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
答案 2 :(得分:1)
我找到了一个可以执行以下操作的解决方案
当你按下edittext键盘显示然后当你按下微调器时它会消失
然后当你在微调器上选择项目时,即使键盘再次显示相同的项目
最后的概率你有什么,如果我按下微调器(所以键盘消失)然后我按屏幕上的任何地方(我希望键盘重新出现)这很容易只是使全局布尔设置为真每次你使键盘出现和副反之亦然,为活动中的所有视图制作onClickListener,除了spinner ofcourse和onClickListener有以下代码
boolean keyboardIsVisible; // which is true if keyboard is visible
if (!keyboardIsVisible) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
这里的代码解决了除最终问题之外的所有问题,因为我已经告诉过你如何解决它
<强> CustomSpinner.java 强>
public class CustomSpinner extends Spinner {
Context mainContext;
public CustomSpinner(Context context) {
super(context);
mainContext = context;
}
public CustomSpinner (Context context, AttributeSet attrs) {
super(context, attrs);
mainContext = context;
}
public CustomSpinner (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mainContext = context;
}
@Override public void
setSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
Activity activity = (Activity) mainContext;
EditText editText = (EditText) activity.findViewById(R.id.edit_text);
InputMethodManager imm = (InputMethodManager)
activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
}
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
CustomSpinner customSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit_text);
button = (Button) findViewById(R.id.button);
customSpinner = (CustomSpinner) findViewById(R.id.spinner);
customSpinner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
editText.setEnabled(false);
editText.setEnabled(true);
return false;
}
});
customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
int count = 0;
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (count > 0){
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}else count++;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// Do Nothing
}
});
}
}