0

这是一种转发,对于任何违反规则我深表歉意,但我对 Android 手机上的软键盘有几个问题:

1)我有一个Android应用程序有几个不同的视图(用户在它们之间切换)。我如何确定哪个是当前视图?我需要获取当前视图来执行隐藏虚拟键盘的代码。

2) 如何查看当前是否正在显示虚拟键盘(以便过滤各种硬键的操作)?

谢谢,R。

4

1 回答 1

0

1) 公共类 ViewIdentification 扩展 Activity 实现 OnFocusChangeListener{

EditText _edt1;
EditText _edt2;
EditText _edt3;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _edt1 = (EditText)findViewById(R.id.EditText01);
    _edt1.setOnFocusChangeListener(ViewIdentification.this);
    _edt2 = (EditText)findViewById(R.id.EditText02);
    _edt2.setOnFocusChangeListener(ViewIdentification.this);
    _edt3 = (EditText)findViewById(R.id.EditText03);    
    _edt3.setOnFocusChangeListener(ViewIdentification.this);


}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub

       if(v == _edt1 && hasFocus == true){

        Toast.makeText(ViewIdentification.this, "The First EditText is focused now", Toast.LENGTH_LONG).show();

       }else if(v == _edt2 && hasFocus == true){

        Toast.makeText(ViewIdentification.this, "The Second EditText is focused now", Toast.LENGTH_LONG).show();

       }else if(v == _edt3 && hasFocus == true){

        Toast.makeText(ViewIdentification.this, "The Third EditText is focused now", Toast.LENGTH_LONG).show();

       }

}

}

注意:通过这种方式我们可以知道哪个视图被聚焦。

2)

这可以通过计算活动的大小(最后一个焦点视图所在的位置)来完成。

于 2011-08-25T13:21:23.433 回答