TextView handle Long Click, parent handle textview click
TextView handle LongClick, parent handle textview's click
我有一个 RelativeLayout
和 TextView
child,当为文本视图设置长按监听器时,为 parent 布局设置点击监听器,如下所示:
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事件必须return为true,这个导致parentview无法处理onclick listener。
你为什么不在你的相对布局上同时使用 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;
}
});
您还需要将 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>
我有一个 RelativeLayout
和 TextView
child,当为文本视图设置长按监听器时,为 parent 布局设置点击监听器,如下所示:
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事件必须return为true,这个导致parentview无法处理onclick listener。
你为什么不在你的相对布局上同时使用 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;
}
});
您还需要将 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>