我有一个Activity,其中有5个EditTexts.当用户单击第一个EditText时,将打开软键盘以在其中输入一些值.我想在软键盘打开时以及当用户点击第一个EditText时以及当软键盘从后退按钮上的相同EditText关闭时,将其他VIEw的可见性设置为Gone.然后我想将其他VIEw的可见性设置为可见.
从Android上的第一个EditText点击打开软键盘时,是否有任何监听器或回调或任何黑客攻击?
解决方法:
这仅适用于androID:您的活动的windowsoftinputMode在清单中设置为adjustResize.您可以使用布局侦听器来查看键盘是否调整了活动的根布局.
我为我的活动使用类似下面的基类:
public class BaseActivity extends Activity { private VIEwTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new VIEwTreeObserver.OnGlobalLayoutListener() { @OverrIDe public voID onGlobalLayout() { int heightDiff = rootLayout.getRootVIEw().getHeight() - rootLayout.getHeight(); int contentVIEwtop = getwindow().findVIEwByID(Window.ID_ANDROID_CONTENT).gettop(); LocalbroadcastManager broadcastManager = LocalbroadcastManager.getInstance(BaseActivity.this); if(heightDiff <= contentVIEwtop){ onHIDeKeyboard(); Intent intent = new Intent("KeyboarDWillHIDe"); broadcastManager.sendbroadcast(intent); } else { int keyboardHeight = heightDiff - contentVIEwtop; onShowKeyboard(keyboardHeight); Intent intent = new Intent("KeyboarDWillShow"); intent.putExtra("KeyboardHeight", keyboardHeight); broadcastManager.sendbroadcast(intent); } } }; private boolean keyboardListenersAttached = false; private VIEwGroup rootLayout; protected voID onShowKeyboard(int keyboardHeight) {} protected voID onHIDeKeyboard() {} protected voID attachKeyboardListeners() { if (keyboardListenersAttached) { return; } rootLayout = (VIEwGroup) findVIEwByID(R.ID.rootLayout); rootLayout.getVIEwTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener); keyboardListenersAttached = true; } @OverrIDe protected voID onDestroy() { super.onDestroy(); if (keyboardListenersAttached) { rootLayout.getVIEwTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener); } }}
以下示例活动使用此选项在键盘显示时隐藏视图,并在隐藏键盘时再次显示键盘.
xml布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/rootLayout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <ScrollVIEw androID:ID="@+ID/scrollVIEw" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" > <!-- omitted for brevity --> </ScrollVIEw> <linearLayout androID:ID="@+ID/bottomContainer" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" > <!-- omitted for brevity --> </linearLayout></linearLayout>
活动:
public class TestActivity extends BaseActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.test_activity); attachKeyboardListeners(); } @OverrIDe protected voID onShowKeyboard(int keyboardHeight) { // do things when keyboard is shown bottomContainer.setVisibility(VIEw.GONE); } @OverrIDe protected voID onHIDeKeyboard() { // do things when keyboard is hIDden bottomContainer.setVisibility(VIEw.VISIBLE); } }
总结 以上是内存溢出为你收集整理的Android中的活动中的软键盘打开和关闭监听器全部内容,希望文章能够帮你解决Android中的活动中的软键盘打开和关闭监听器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)