我目前正在尝试布局和打开新页面,我想使用OnLongClickListener打开一个页面,但使用相同的文本视图框打开带有OnClickListener的Toast。
这是我迄今为止设计的。它不会起作用,因为长按需要输出布尔值。
任何想法?
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第二页
<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;
}
});
}
}
答案 0 :(得分:0)
我找到了解决自己问题的方法。
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;
}
});
}