• 长按事件的处理


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

    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}
    ";
                    if(str.matches(argex))
                    {
                        tv_2.setText("true");
                    }
                    else{
                        tv_2.setText("false");
                    }
                }
            });
        }
    }
  • 相关阅读:
    UML中的构件图
    Extjs4 中的gird
    【转载】C#实现线程安全的网络流
    [转载]组播
    【转载】wcf综合运用之:大文件异步断点续传
    【转载】WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
    【转载】特殊用途的IP地址介绍
    [转载]C# HashTable
    c宏定义实战
    c异或加密与解密
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9001319.html
Copyright © 2020-2023  润新知