setOnLongClickListener 返回值的作用

小菜鸟战斗机 2018-3-27 223

直接说结果 : 返回值是false(默认)  则长按时执行完长按监听之后会走点击的监听

返回值是true   则长按时只会执行setOnLongClickListener


下面是验证,可以不看- -。

布局文件

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:orientation="vertical"  
  9.     tools:context="com.example.administrator.zzz.MainActivity">  
  10.   
  11.     <Button  
  12.         android:id="@+id/btn1"  
  13.         android:text="返回值为false"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"/>  
  16.     <Button  
  17.         android:id="@+id/btn2"  
  18.         android:text="返回值为true"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"/>  
  21.   
  22. </LinearLayout>  


MainActivity

[java] view plain copy
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {  
  2.   
  3.     private Button btn1, btn2;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.   
  10.         btn1 = (Button) findViewById(R.id.btn1);  
  11.         btn2 = (Button) findViewById(R.id.btn2);  
  12.   
  13.         btn1.setOnClickListener(this);  
  14.         btn2.setOnClickListener(this);  
  15.         btn1.setOnLongClickListener(this);  
  16.         btn2.setOnLongClickListener(this);  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onClick(View view) {  
  21.         Toast.makeText(this"短按", Toast.LENGTH_SHORT).show();  
  22.     }  
  23.   
  24.     @Override  
  25.     public boolean onLongClick(View view) {  
  26.         switch (view.getId()) {  
  27.             case R.id.btn1:  
  28.                 Toast.makeText(this"长按", Toast.LENGTH_SHORT).show();  
  29.                 return false;  
  30.             case R.id.btn2:  
  31.                 Toast.makeText(this"长按", Toast.LENGTH_SHORT).show();  
  32.                 return true;  
  33.         }  
  34.         return true;  
  35.     }  
  36. }  


最新回复 (0)
返回