长按事件的处理

(一)实现效果:长按两秒将图片设置成背景图

1,布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="helloworld.com.inspur.demo5.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_show"
        android:text="点击设置背景"
        />

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/im"
        android:src="@drawable/a"
        />



</LinearLayout>

2,MainActivity实现逻辑代码

package helloworld.com.inspur.demo5;

import android.graphics.drawable.Drawable;
import android.nfc.TagLostException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.graphics.drawable.DrawableWrapper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        textView=(TextView)findViewById(R.id.tv_show);
        imageView=(ImageView)findViewById(R.id.im);
        textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                imageView.setBackgroundResource(R.drawable.a);
                textView.setText("hahhah");
                return true;
            }
        });
       
    }
}

3,测试public void onClick(View v)中返回值的区别

true:事件已经完全处理,不需要其他的函数继续处理

false:事件未完全处理,还需要继续处理

 textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                imageView.setBackgroundResource(R.drawable.a);
                textView.setText("hahhah");
                return true;
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  Toast.makeText(MainActivity.this,"hahh",Toast.LENGTH_SHORT);
                textView.setText("111111");
            }
        });

4,改成长按设置成壁纸

(1)设置权限:在application上面加入代码:

  <uses-permission android:name="android.permission.SET_WALLPAPER"/>

(2)编写逻辑代码

textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                imageView.setBackgroundResource(R.drawable.a);
                setWallPaper();
                textView.setText("hahhah");
                return true;
            }
        });

 

public void setWallPaper()
    {
        try {
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
            wallpaperManager.setResource(R.drawable.a);
        }
        catch(Exception e)
        {

        }
    }

 

会报错,但是可以正常运行

 

 (二)输入邮箱,判断是否正确

1,布局(activity_main)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="helloworld.com.inspur.app2.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入邮箱"
        android:id="@+id/tv_1"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="第一个输入框"
        android:id="@+id/ed_1"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="第二个输入框"
        android:id="@+id/ed_2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_2"/>
</LinearLayout>

 

2,activity编写逻辑代码(MainActivity)

package helloworld.com.inspur.app2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private EditText et_1;
    private TextView tv_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_1=(EditText)findViewById(R.id.ed_1);
        tv_2=(TextView)findViewById(R.id.tv_2);
        et_1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String str=et_1.getText().toString();
                String argex="\\\\w+@\\\\w+(\\\\.\\\\w{2,3})*\\\\.\\\\w{2,3}\n";
                if(str.matches(argex))
                {
                    tv_2.setText("true");
                }
                else{
                    tv_2.setText("false");
                }
            }
        });
    }
}

 

转载于:https://www.cnblogs.com/excellencesy/p/9001319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 Android 应用中实现事件,可以通过以下步骤进行封装: 1. 给目标 View 设置按监听器: ```java view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 事件处理逻辑 return true; // 返回 true 表示已经消费了该事件 } }); ``` 2. 在事件处理逻辑中添加计时器,以判断是否达到按的时间阈值: ```java private Handler handler = new Handler(); private boolean isLongClick = false; private Runnable longClickRunnable = new Runnable() { @Override public void run() { isLongClick = true; // 事件处理逻辑 } }; view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { isLongClick = false; handler.postDelayed(longClickRunnable, 500); // 事件的时间阈值为 500ms return true; // 返回 true 表示已经消费了该事件 } }); ``` 3. 在目标 View 的 onTouchEvent() 方法中添加 UP 和 CANCEL 事件处理逻辑,以取消计时器和重置状态: ```java view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { handler.removeCallbacks(longClickRunnable); if (isLongClick) { // 事件已经触发,不需要执行其他逻辑 } else { // 点击事件处理逻辑 } isLongClick = false; } return false; } }); ``` 这样就完成了事件的封装,可以在需要的地方直接使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值