我可以使用OnLongClick打开单独的XML布局吗?(Can I open a seperate XML layout with a OnLongClick)

编程入门 行业动态 更新时间:2024-10-24 00:23:58
可以使用OnLongClick打开单独的XML布局吗?(Can I open a seperate XML layout with a OnLongClick)

我目前正在尝试布局和打开新页面,我想用OnLongClickListener打开一个页面,但是使用相同的文本视图框打开带有OnClickListener的Toast。

这是我到目前为止设计的。 它不起作用,因为长按需要输出布尔值。 有任何想法吗?

XML主要

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android.how_to_use.MainActivity" android:id="@+id/main_view"> <TextView android:id="@+id/long_press_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Long Press To Open \n'Drag n Drop'"/> </LinearLayout>

XML第2页

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/swirl_img" android:src="@drawable/Swirl" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

MainActivity.Java

public class MainActivity extends AppCompatActivity { private LinearLayout mainView; private TextView txtView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtView = (TextView) findViewById(R.id.long_press_textView); addListenerLongPressBtn(); addListenerOnClickBtn(); } public void addListenerOnClickBtn() { txtView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show(); } }); } public void addListenerLongPressBtn() { txtView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Intent intent = new Intent(this, DragDropActivity.class); startActivity(intent); return true; } }); } }

I am currently experimenting with layouts and opening new pages, I would like to open one page with a OnLongClickListener but a toast with a OnClickListener using the same text view box.

This is what I've designed so far. It won't work as the long click needs to output a boolean. Any Ideas?

XML main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android.how_to_use.MainActivity" android:id="@+id/main_view"> <TextView android:id="@+id/long_press_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Long Press To Open \n'Drag n Drop'"/> </LinearLayout>

XML 2nd page

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/swirl_img" android:src="@drawable/Swirl" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

MainActivity.Java

public class MainActivity extends AppCompatActivity { private LinearLayout mainView; private TextView txtView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtView = (TextView) findViewById(R.id.long_press_textView); addListenerLongPressBtn(); addListenerOnClickBtn(); } public void addListenerOnClickBtn() { txtView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show(); } }); } public void addListenerLongPressBtn() { txtView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Intent intent = new Intent(this, DragDropActivity.class); startActivity(intent); return true; } }); } }

最满意答案

我找到了解决自己问题的方法。

public void addListenerLongPressBtn() { txtView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Intent intent = new Intent(view.getContext(), DragDropActivity.class); startActivity(intent); return true; } }); }

I found a solution to my own problem.

public void addListenerLongPressBtn() { txtView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Intent intent = new Intent(view.getContext(), DragDropActivity.class); startActivity(intent); return true; } }); }

更多推荐

本文发布于:2023-08-07 21:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   布局   OnLongClick   layout   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!