将触摸事件从父视图传递到特定子视图

时间:2017-11-20 22:32:18

标签: android android-layout

我有一个android视图布局结构如下

<RelativeLayout>
 <TextView>
 <EditText>
</RelativeLayout>

由于我的布局技巧有限,EditText的大小为wrap_content,并且明显小于父RelativeLayout。

但是当用户触及RelativeLayout时,我希望UI有效地表现得好像用户只是专注于EditText。

如果光标在前面,中间或末尾开始,对我来说并不重要。 (最好在结束时)。

这是我可以在代码或布局中实现的东西吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以做的是在相对布局的onClick方法中调用editText的onClick。这样,relativeLayout上的所有点击都会转到editText

这应该可以使用answer

relativeLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        //needed for some older devices.
        InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
});

我会以编程方式(在代码中)执行此操作,因为它不处理视图的外观,而是它们的行为方式。因此,比如在MVC中,它属于控制器。

相关问题