1

I'm creating a compose screen for my app. I have a ScrollView which contains a RelativeView which in turn contains two things: the EditText where the user types a message, and an ImageView whose visibility is toggled on and off depending on whether an image is attached to the status or not. Here's that part of my layout 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>

Because my EditText's height is wrap_content, the whole thing starts off with a single line of gray background (the EditText) on top of a yellow background (the RelativeLayout, which fully covers the green background of the ScrollView). However, I'll later change all the views' backgrounds to white (to make them look like a single component) and it will be counter-intuitive for the user to be able to tap only that single line of EditText to make the keyboard pop up.

enter image description here

What I want to do is to redirect the click and long click actions of the RelativeLayout to the click and long click actions of the EditText, but my code below doesn't work. Help?

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

My intention here is so that when the RelativeLayout is clicked, the keyboard pops up (as it does when done to an EditText) and when long-pressed, display the cut/copy/paste/text selection options (same behavior with EditText).

4

2 回答 2

1

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

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

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

编辑:你也必须打电话editText.requestFocus();(@Phil 是对的),但根据我的经验,打开键盘是不够的,所以你还需要上面丑陋的代码。

于 2013-05-02T14:13:06.420 回答
0

你想要的是让你EditText获得焦点(这反过来会导致键盘显示):

rl.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Logger.d("onClick invoked!");
        editText.requestFocus();
    }
});
rl.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Logger.d("onLongClick invoked!");
        editText.requestFocus();
        return true;
    }
});
于 2013-05-02T14:20:30.950 回答