选择editText时如何隐藏部分布局

时间:2015-08-02 20:16:13

标签: 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/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键入一些值时,我需要隐藏linearLayout android:id =&#34; @ + id / InnerRelativeLayout&#34;因为否则它仍然可以在键盘上方看到。

2 个答案:

答案 0 :(得分:1)

需要将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 } } }); ,如下所示:

for (int i = 0; i < checkedMonthsBox.Items.Count; i++)
        {
            if (checkedMonthsBox.GetItemCheckState(i) == CheckState.Checked)
            {
                months.Add(checkedMonthsBox.Items[i].ToString());
            }
         }

答案 1 :(得分:0)

实际上并非如此简单,这是我的解决方案,我的工作方式。

首先需要创建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" />

    <!-- change yourapp -->
    <com.yourapp.MyEditText
        android:id="@+id/tariffName"
        android:gravity="right"
        android:padding="3dip" />

android:focusable =&#34; true&#34; 添加到 LinearLayout

<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">

然后加入您的活动,例如 MainActivity.java

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);
}