我可以使用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; } }); }更多推荐
发布评论