0

我有RelativeLayout一个TextView孩子,当为文本视图设置长点击侦听器时,为父布局设置点击侦听器如下:

textview.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(getContext(), "longclick", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "layout onclick", Toast.LENGTH_SHORT).show();
            }
        });

单击 textview 时如何处理布局的单击侦听器?我也GestureDetector用来处理,但是ondown事件必须返回true,这导致父视图无法处理onclick监听器。

4

2 回答 2

0

你为什么不在你的相对布局上同时使用 ClickListener,一个长的和一个常规的。

layout.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

   Toast.makeText(HomeSafeActivity.this, "Normal Press", Toast.LENGTH_LONG).show();

});

layout.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();

        return true;
    }
});
于 2018-04-27T09:09:48.737 回答
0

您还需要设置ClickListener()为您的textview

检查以下经过测试的示例,它将对您有用

尝试这个

public class MainActivity extends AppCompatActivity {


    TextView mTitle;
    LinearLayout toolbarIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = (TextView) findViewById(R.id.toolbar_title);
        toolbarIcon = (LinearLayout) findViewById(R.id.toolbarIcon);
        mTitle.setText("Nilesh Rathod");


        mTitle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                View parent = (View) v.getParent();
                parent.performClick();
            }
        });

        mTitle.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(MainActivity.this, "mTitle Clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        toolbarIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "toolbarIcon Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbarIcon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">


    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:textStyle="bold" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Nilesh"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Nilesh"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Nilesh"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Nilesh"
        android:textStyle="bold" />


</LinearLayout>
于 2018-04-27T09:19:34.587 回答