android - 如何从RelativeLayout调用EditText的onClick和onLongClick

标签 android android-layout android-edittext android-relativelayout onclicklistener

我正在为我的应用程序创建一个撰写屏幕。我有一个 ScrollView其中包含 RelativeView这又包含两件事:EditText用户键入消息的位置,以及 ImageView其可见性根据图像是否附加到状态来打开和关闭。这是我的布局 XML 的一部分。

<!-- @dimen/bigGap = 8dp -->
<ScrollView android:id="@+id/parentScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_marginTop="@dimen/bigGap"
    android:layout_marginRight="@dimen/bigGap"
    android:layout_marginLeft="@dimen/bigGap"
    android:layout_marginBottom="@dimen/bigGap"
    android:layout_above="@+id/footer"
    android:background="#006400"
    > <!-- green background color -->

    <RelativeLayout android:id="@+id/parentLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFD700"> <!-- yellow background color -->

        <EditText android:id="@+id/postText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#dddddd"
            android:inputType="textMultiLine"
            android:gravity="top|left"
            /> <!-- gray background color -->

        <ImageView android:id="@+id/postImage"
            android:layout_width="@dimen/thumbnailSize"
            android:layout_height="@dimen/thumbnailSize"
            android:visibility="gone"
            android:layout_below="@id/postText"
            />
    </RelativeLayout>
</ScrollView>

因为我的EditText的高度是wrap_content , 整个事情从黄色背景( EditText ,完全覆盖 RelativeLayout 的绿色背景)顶部的单行灰色背景( ScrollView )开始。但是,稍后我会将所有 View 的背景更改为白色(使它们看起来像一个单独的组件)并且用户只能点击 EditText 的那一行是违反直觉的。使键盘弹出。

enter image description here

我想做的是重定向 RelativeLayout 的点击和长按操作EditText 的点击和长按操作,但我下面的代码不起作用。帮忙?

final EditText editText = (EditText) findViewById(R.id.postText);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.parentLinearLayout);
rl.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Logger.d("onClick invoked!");
        editText.performClick();
    }
});
rl.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Logger.d("onLongClick invoked!");
        return editText.performLongClick();
    }
});

我的 Intent 是当 RelativeLayout单击时,键盘会弹出(就像对 EditText 所做的那样),当长按时,会显示剪切/复制/粘贴/文本选择选项(与 EditText 的行为相同)。

最佳答案

从描述来看,您真正想要的是打开键盘。所以你的问题标题暗示了一个解决方案,而不是实际问题。

从您的点击监听器调用它(或在您显示页面时立即调用):

  ((InputMethodManager) myActivity
            .getSystemService(Context.INPUT_METHOD_SERVICE))
            .toggleSoftInput(InputMethodManager.SHOW_FORCED,
                    InputMethodManager.HIDE_IMPLICIT_ONLY);

编辑: 您还必须调用 editText.requestFocus();(@Phil 是对的),但根据我的经验,打开键盘还不够,因此您还需要上面的丑陋代码。

关于android - 如何从RelativeLayout调用EditText的onClick和onLongClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339777/

相关文章:

android - 如何在android中动态启动和完成progressBar

android - 如何在Alert Dialog中设置EditText的dp左右边距

java - 如何设置警告框

android - 谷歌地图 Android 授权失败

java - 将方法 'YoutubeRecyclerAdapter' 的第二个参数从 'Context' 更改为 'Callback<...>'

java - 为什么颜色改变时按钮会改变大小?

android - 是否建议将 Spinner 用于布局更改

Android 如何使用 InputType ="text"的 EditText 显示数字软键盘

android - DialogFragment 在方向改变时消失

java - 我如何更新 ListItem (LiveData) 的字段