TextView处理LongClick,父句柄textview的单击

时间:2018-04-27 09:01:05

标签: android long-click

我为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监听器。

2 个答案:

答案 0 :(得分:0)

为什么不在相对布局上使用ClickListener,One long和one regular。

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

答案 1 :(得分: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>