setOnLongClickListener 返回值的作用

来源:互联网 发布:java web开发与实战 编辑:程序博客网 时间:2024/06/07 21:14

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

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


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

布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.administrator.zzz.MainActivity">    <Button        android:id="@+id/btn1"        android:text="返回值为false"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <Button        android:id="@+id/btn2"        android:text="返回值为true"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {    private Button btn1, btn2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn1 = (Button) findViewById(R.id.btn1);        btn2 = (Button) findViewById(R.id.btn2);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn1.setOnLongClickListener(this);        btn2.setOnLongClickListener(this);    }    @Override    public void onClick(View view) {        Toast.makeText(this, "短按", Toast.LENGTH_SHORT).show();    }    @Override    public boolean onLongClick(View view) {        switch (view.getId()) {            case R.id.btn1:                Toast.makeText(this, "长按", Toast.LENGTH_SHORT).show();                return false;            case R.id.btn2:                Toast.makeText(this, "长按", Toast.LENGTH_SHORT).show();                return true;        }        return true;    }}