Editview按下软键返回,失去焦点并隐藏键盘,怎么样?

时间:2013-04-02 21:37:40

标签: android textview

我正在开发Android应用,我有2个editviews和一个标签。用户可以输入2个值,标签显示使用editviews输入的一些计算。我想要的是以下内容;

  1. 用户使用软键盘输入任一值
  2. 用户按下“返回”软键
  3. editview应该失去焦点
  4. 软键盘应该消失
  5. textview标签应重新计算
  6. 现在,当有另一个可以获得焦点的小部件(?)时,v.clearFocus似乎才有效,所以我还添加了一个可以“窃取”的 dummie零像素布局 '从第一个编辑视图的焦点。 Return键现在可以正常工作,但是当用户通过简单地点击将焦点从edit1切换到edit2时, HideKeyboard()崩溃。我已经尝试检查inputMethodManager == null但是没有帮助。

    这一切都让我觉得我是在欺骗Android来做一些常见的UI行为,所以我不禁想到我在这里忽略了一些东西。

    非常感谢任何帮助。顺便说一句,我知道这与这个问题类似:How to lose the focus of a edittext when "done" button in the soft keyboard is pressed? 但我已经尝试过了,但它不起作用。

    所以我的布局xml是这样的:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <!-- Dummy control item so that first textview can lose focus -->
        <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="0px"
            android:layout_height="0px"/>
    
        <EditText
            android:id="@+id/editTest1"
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:imeOptions="actionDone" >
    
        </EditText>
    
    
        <EditText
            android:id="@+id/editTest2"
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:imeOptions="actionDone" >
        </EditText>
    
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test123" />
    
    </LinearLayout>
    

    源头就是这样:

    public class CalcActivity extends Activity implements OnFocusChangeListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tab2_weight);
    
            EditText testedit = (EditText) findViewById(R.id.editTest1);
    
            testedit.setOnFocusChangeListener(this);
    
            testedit.setOnEditorActionListener(new OnEditorActionListener() {        
    
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if(actionId==EditorInfo.IME_ACTION_DONE){
                        //Clear focus here from edittext
                        Log.d("test app", "v.clearFocus only works when there are other controls that can get focus(?)");
                        v.clearFocus();
                    }
                return false;
                }
            });
        }
    
        public void hideSoftKeyboard() {
            InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
    
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus == false) {
                Log.d("unitconverter", "onFocusChange hasFocus == false");
                // update textview label
                TextView bla = (TextView) findViewById(R.id.textView1);
                bla.setText(String.format("%s + %s", (((EditText) findViewById(R.id.editTest1)).getText()), (((EditText) findViewById(R.id.editTest2)).getText()))); 
                // hide keyboard
                hideSoftKeyboard();
            }
        }
    
    }
    

0 个答案:

没有答案