在按下打开键盘按钮期间隐藏LinearLayout一次

时间:2016-05-15 09:40:27

标签: android

我不知道如何安排这个。已经尝试了许多方法 我需要之后,锁定键盘隐藏LinerLayout,这段代码不起作用。

我该如何安排?

CustomLinearLayout.class

上下文package my.package; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; public class CustomLinearLayout extends LinearLayout { Context context; public CustomLinerLayout(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { LinearLayout menu = (LinearLayout) findViewById(R.id.menu); menu.setVisibility(GONE); } return false; } }

Profile.class

package my.package; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewTreeObserver; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; import java.lang.reflect.Array; public class Profile extends AppCompatActivity { CustomLinearLayout menu; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_application); menu = (CustomLinearLayout) findViewById(R.id.menu); menu.setVisible(View.VISIBLE); } }

public class Notifications
{
    public Notifications(AppSettings settings) {
        this.settings = settings;
    }

    public void SendEmail(string subject, string body) {
        SmptClient.Send(subject, body, settings["email address"]);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题是你的CustomLinearLayout没有成为焦点,因此它不会收到任何键盘事件。你可以做的是使它可以在xml或像这样的代码中聚焦:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_application);
    menu = (CustomLinearLayout) findViewById(R.id.menu);
    menu.setVisible(View.VISIBLE);

    menu.setFocusable(true);
    menu.setFocusableInTouchMode(true);
}

同样在你的布局类中,如果处理了事件以阻止系统处理它,你需要返回true

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        LinearLayout menu = (LinearLayout) findViewById(R.id.menu);
        menu.setVisibility(GONE);

        return true;
    }

    return false;
}

但是,如果您EditText

之后重点关注onCreate,那么这对您无济于事