在android问题中单击外部编辑文本关闭键盘
我拥有的::在android问题中单击外部编辑文本关闭键盘,android,Android,我拥有的:: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
- 我有两个容器,每个容器都有一个edittext小部件
- 当我点击编辑文本时,键盘弹出
- 我想关闭从editText更改的焦点上的编辑文本(如果 在编辑文本外单击)
- Edittext未关闭
MainActivity.java
public class MainActivity extends Activity {
FragmentTransaction ft;
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load header and body fragment to the container
ft = getFragmentManager().beginTransaction();
Fragment fragHeader = ContainerOne.newInstance();
Fragment fragBody = ContainerTwo.newInstance();
//Header
ft.replace(R.id.containerOne, fragHeader, ContainerOne.class.getSimpleName());
//Body
ft.replace(R.id.containerTwo, fragBody, ContainerTwo.class.getSimpleName());
ft.commit();
}
}
public class ContainerOne extends Fragment {
EditText editText1;
public static ContainerOne newInstance(){
Log.d(ContainerOne.class.getSimpleName(), "newInstance");
ContainerOne fragment = new ContainerOne();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Make sure the keyboard only pops up when a user clicks into an EditText
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.container_one, container, false);
editText1=(EditText) rootView.findViewById(R.id.editText1);
return rootView;
}
@Override
public void onStart() {
super.onStart();
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
}
public void hideKeyboard(View view) {
//Hiding the keyboard onClick of outside the edittext
InputMethodManager inputMethodManager =(InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
public class ContainerTwo extends Fragment {
public static ContainerTwo newInstance(){
Log.d(ContainerTwo.class.getSimpleName(), "newInstance");
ContainerTwo fragment = new ContainerTwo();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.container_two, container, false);
return rootView;
}
}
活动\u main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/containerOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/containerTwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
container_one.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/containerOne"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/containerTwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
我怎样才能解决这个问题
我已经添加了所有代码在您的容器上添加一个单击侦听器(在编辑文本之外),并调用您的hideKeyboard方法仅在实用程序类或主要活动中定义一个方法hideSoftKeyboardOnTouch(),如下所示。最好在实用程序类中定义它,因为它可以从不同的类中使用(就像我在项目中所做的那样)
请您在表单中有多个编辑文本,然后您需要使用下面的代码
edtText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(view);
} else {
showKeyboard(view);
}
}
});
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public void showKeyboard(View view) {
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view,0);
}
你说的“关闭编辑文本”是什么意思?当你点击编辑文本时,键盘会弹出。。。当键盘弹出时,我想在编辑文本外单击关闭键盘
edtText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(view);
} else {
showKeyboard(view);
}
}
});
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public void showKeyboard(View view) {
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view,0);
}