EditText.requestFocus()不能与onKeyListener一起使用

时间:2016-08-16 22:29:37

标签: android android-edittext onkeylistener

我试图将焦点设置在EditText(inputOne)上,但我的代码显示奇怪的行为。当我按下按钮时,inputOne获得焦点,一切都很好。如果我按"输入"在SoftKeyboard上,触发onKeyListener,inputTwo仍然是焦点。在这两种情况下" inputOne.requestFocus"返回true。 这是我的代码:

public class EditSeriesActivity extends Activity {

private FlashcardSeries series;

private EditText inputOne;
private EditText inputTwo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editseries);
    Intent intent = getIntent();
    series = intent.getParcelableExtra("EXTRA_MASSAGE");
    TextView textView = (TextView) findViewById(R.id.nameseries);
    textView.setText(series.getName());
    inputOne = (EditText) findViewById(R.id.side1);
    inputTwo = (EditText) findViewById(R.id.side2);
    inputTwo.setOnKeyListener(new View.OnKeyListener()
    {


        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        addCardToSeries(v);
                        break;
                    default:
                        break;
                }
            }
            return false;
        }
    });

}

public void addCardToSeries(View view){
    series.addFlashcard(new Flashcard(inputOne.getText().toString(), inputTwo.getText().toString()), this);

    inputTwo.setText("");
    inputOne.setText("");
    inputOne.requestFocus();

}
}

1 个答案:

答案 0 :(得分:0)

试试这个

inputOne.requestFocus();
InputMethodManager inpuMethod = (InputMethodManager) getSystemService(G.context.INPUT_METHOD_SERVICE);
inpuMethod.showSoftInput(inputOne, InputMethodManager.SHOW_IMPLICIT);