现在的位置: 首页 > 移动开发 > 正文

android 长按事件

2019年07月21日 移动开发 ⁄ 共 2676字 ⁄ 字号 评论关闭

按住2秒后才有所操作

 

在main.xml中的代码如下:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

   <TextView

       android:id="@+id/info"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:text="长按图片,将图片为背景"/>

   <ImageView

       android:id="@+id/img"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:src="@drawable/mldn_bg"/>

</LinearLayout>

 

 

在MyLongClickDemo.java中的代码如下:

 

package com.tarena.longclick;

 

import java.io.IOException;

 

import android.app.Activity;

import android.content.res.Resources.NotFoundException;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnLongClickListener;

import android.widget.ImageView;

import android.widget.TextView;

 

public class MyLongClickDemo extends Activity {

  private TextView info = null;

  private ImageView img = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        this.info = (TextView) super.findViewById(R.id.info);

        this.img = (ImageView) super.findViewById(R.id.img);

        this.img.setOnLongClickListener(new OnLongClickListenerImpl());

    }

    private class OnLongClickListenerImpl implements OnLongClickListener{

 

     public boolean onLongClick(View v) {

       try {                                   //生成try catch,先选中要try catch的部分,再右键Surround With --> Try/catch Block

         MyLongClickDemo.this.clearWallpaper();  //清除已有的桌面

         MyLongClickDemo.this.setWallpaper(MyLongClickDemo.this.img

              .getResources().openRawResource(R.drawable.mldn_bg)); // 设置桌面

         MyLongClickDemo.this.info.setText("手机桌面背景已修改!");

       } catch (Exception e) {

         MyLongClickDemo.this.info.setText("手机桌面背景设置失败");

       }

       return false;

     }

   

    }

}

 

 

 

然后还要在AndroidManifest.xml中修改权限:

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.tarena.longclick"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk android:minSdkVersion="10" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

            android:name=".MyLongClickDemo"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

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

</manifest>

抱歉!评论已关闭.