【问题标题】:How can i create a toast when longpress anywhere in the view in android?在android视图中的任何地方长按时如何创建吐司?
【发布时间】:2014-07-01 07:02:01
【问题描述】:

您好,当我们长按应用程序视图中的任意位置时,我需要创建一个 toast。

我的祝酒词是:

Toast.makeText(getApplicationContext(), "Long Pressed", Toast.LENGTH_SHORT).show();

任何人都可以帮助我吗?提前致谢。

【问题讨论】:

  • 点击你的根布局你需要显示吐司。

标签: android long-press


【解决方案1】:
  myView = findViewById(r.id.my_view);
  myView.setOnLongClickListener(new OnLongClickListener() {
    public boolean onLongClick(View arg0) {
  Toast.makeText(getApplicationContext(), "Long Clicked " ,Toast.LENGTH_SHORT).show();

        return true;    // set to true
    }
});

【讨论】:

  • 这对我的申请不公平。我需要长按当前活动本身。我需要在视图中长按。如果还有其他方法吗???
  • 只需通过 yourViewOrLayout 简单地更改按钮。
【解决方案2】:

你的类可以使用 onLongClickListener 接口。

您的类扩展了 Activity 实现 View.OnLongClickListener 具有在长按时获得通知的方法。

别忘了设置 yourView.setOnLongClickListener(this);获取您需要的所有视图。

【讨论】:

    【解决方案3】:

    例如,如果您有像 LinearLayout 这样的顶部布局,请在 LongClick 上设置:

    你的 layout.xml

      <LinearLayout
    android:id="@+id/your_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    

    你的 Activity.java

     LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_layout);
    
     yourLayout.setOnLongClickListener(new OnLongClickListener() {
    
        @Override
           public boolean onLongClick(View v) {
             Toast.makeText(yourMainActivity.this, "Your Text", Toast.LENGTH_SHORT).show();
             return true;
         }
    
         });
    

    【讨论】:

      【解决方案4】:

      在 android 视图中的任意位置长按时创建 toast -

      长按视图 ===>


      LongPress.java 类 -

      public class LongPress extends Activity {
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_long_press);
        TextView txtView = (TextView) findViewById(R.id.txtView);
        txtView.setOnLongClickListener(new OnLongClickListener() {
         @Override
         public boolean onLongClick(View v) {
          // TODO Auto-generated method stub
          Toast.makeText(getApplicationContext(),
            "You have pressed it long :)", 2000).show();
          return true;
         }
        });
        txtView.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
          // TODO Auto-generated method stub
          Toast.makeText(getApplicationContext(), "Not Long Enough :(",
            1000).show();
         }
        });
       }
      
      }
      

      相关布局文件-

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      tools:context=".LongPress" >
      
      <TextView
      android:id="@+id/txtView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:gravity="center"
      android:text="Long Press Me!!!!" />
      
      </RelativeLayout>
      

      来源:android-long-press-event-handle-example

      【讨论】:

        猜你喜欢
        • 2014-05-13
        • 2016-03-29
        • 1970-01-01
        • 2022-07-22
        • 2017-06-29
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多