我有什么::
发生了什么:
单个容器不存在此问题,如何解决此问题
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();
}
}
activity_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>
ContainerOne.java
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);
}
}
container_one.xml
<?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_two:
<?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>
ContainerTwo.java
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;
}
}
我该如何解决这个问题!
答案 0 :(得分:1)
在实用程序类或主活动中定义一个方法hideSoftKeyboardOnTouch()(适用于您),如下所示。它最好在实用程序类中定义它,因为它可以在不同的类中使用(就像我在我的项目中所做的那样)。
public static void hideKeyboardOnTouch(final Activity activity, View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(activity);
return false;
}
});
}
// If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
hideKeyboardOnTouch(activity, innerView);
}
}
}
将id设置为父布局(在您的情况下为线性布局),假设id为parent
<LinearLayout
android:id="@+id/parent"
-----
-----
</LinearLayout>
然后在你的主要活动onCreate()中调用这个hideSoftKeyboardOnTouch()方法,像这样传递它的上下文和父布局id
hideSoftKeyboardOnTouch(this, findViewById(R.id.parent));
答案 1 :(得分:0)
在容器上(编辑文本之外)添加一个点击监听器并调用hideKeyboard方法
答案 2 :(得分: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);
}