Java 选择editText时如何隐藏部分布局
当我点击EditText和keybord显示时,有没有办法隐藏一些按钮 我有这个布局Java 选择editText时如何隐藏部分布局,java,android,android-layout,Java,Android,Android Layout,当我点击EditText和keybord显示时,有没有办法隐藏一些按钮 我有这个布局 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/InnerRelativeLayo
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/InnerRelativeLayout">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/table">
<TableRow
android:id="@+id/tariffRow">
<TextView
android:layout_column="1"
android:text="Název"
android:padding="3dip" />
<EditText
android:id="@+id/tariffName"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout android:id="@+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button android:id="@+id/okBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Ok"/>
<Button android:id="@+id/stornoBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Storno" />
</LinearLayout>
现在,当单击editText以键入某些值时,我需要隐藏该线性布局android:id=“@+id/InnerRelativeLayout”,因为否则它在键盘上方仍然可见。需要将
FocusChangeListener
设置为editText
,只要焦点在该editText
上发生变化,就会触发,如下所示:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// hide relative layout
} else {
// show relative layout
}
}
});
其实并不是那么简单,这是我的解决方案,我是如何让它工作的 首先需要创建MyEditText.java
public class MyEditText extends EditText {
private KeyImeChange keyImeChangeListener;
public MyEditText(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public void setKeyImeChangeListener(KeyImeChange keyImeChange){
keyImeChangeListener = keyImeChange;
}
public interface KeyImeChange {
public boolean onKeyIme(int keyCode, KeyEvent keyEvent);
}
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent keyEvent){
if(keyImeChangeListener != null){
return keyImeChangeListener.onKeyIme(keyCode, keyEvent);
}
return false;
}
}
然后layout.xml,更改以下内容
<EditText
android:id="@+id/tariffName"
android:gravity="right"
android:padding="3dip" />
还要添加这些来完成实际工作
public void removeFocus() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.requestFocus();
}
public void showLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.VISIBLE);
}
public void hideLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.INVISIBLE);
}
我的答案对你有用吗?这实际上不起作用,请检查我下面的决议。事实上,这是有效的,不需要按照你的方式来做
<LinearLayout android:id="@+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:focusable="true">
import fi.hgs.apps.MyEditText.KeyImeChange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
removeFocus();
MyEditText myEditText = (MyEditText) findViewById(R.id.tariffName);
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
showLinearLayout();
} else {
hideLinearLayout();
}
}
});
myEditText.setKeyImeChangeListener(new KeyImeChange() {
@Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
removeFocus();
}
return false;
}
});
}
public void removeFocus() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.requestFocus();
}
public void showLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.VISIBLE);
}
public void hideLinearLayout() {
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.InnerRelativeLayout);
linearLayout.setVisibility(View.INVISIBLE);
}