在布局文件中增加父控件属性android:focusable="true"试一下呢

解决方案 »

  1.   

    在布局文件中加了android:focusable="true"也不管用,还是没有跑到onFocusChange。
    我的EDITTEXT是一个搜索输入框,做一个百度地图的应用,输入完后想实现点击其他地方隐藏输入法
      

  2.   

    EditText输入完成以后,不用点击按钮吗?譬如“提交”、“搜索”什么的。
      

  3.   

    我测试了一下<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:focusable="true" 
        android:focusableInTouchMode="true">    <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/></LinearLayout>
    public class MainActivity extends Activity {
    private EditText _input;
    private LinearLayout _linear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _input = (EditText)findViewById(R.id.input);
    /*_input.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View arg0, boolean arg1) {
    Toast.makeText(getApplicationContext(), "EditText焦点改变",
         Toast.LENGTH_SHORT).show();
    }
    });*/
    _linear = (LinearLayout)findViewById(R.id.linear);
    _linear.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
    _linear.requestFocus();
    Toast.makeText(getApplicationContext(), "Layout获取焦点",
         Toast.LENGTH_SHORT).show();
    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
    if (inputManager.isActive()) { 
    inputManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 

    return false;
    }
    });
    }
    这样可以实现。
      

  4.   

    我测试了一下<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:focusable="true" 
        android:focusableInTouchMode="true">    <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/></LinearLayout>
    public class MainActivity extends Activity {
    private EditText _input;
    private LinearLayout _linear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _input = (EditText)findViewById(R.id.input);
    /*_input.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View arg0, boolean arg1) {
    Toast.makeText(getApplicationContext(), "EditText焦点改变",
         Toast.LENGTH_SHORT).show();
    }
    });*/
    _linear = (LinearLayout)findViewById(R.id.linear);
    _linear.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
    _linear.requestFocus();
    Toast.makeText(getApplicationContext(), "Layout获取焦点",
         Toast.LENGTH_SHORT).show();
    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
    if (inputManager.isActive()) { 
    inputManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 

    return false;
    }
    });
    }
    这样可以实现。谢谢,按照类似的方法改了