Android学习之长按事件
桌面设置授权操作:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
MyLongClickDemo.java:
import android.app.Activity; 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.img = (ImageView) super.findViewById(R.id.img); this.info = (TextView) super.findViewById(R.id.info); this.img.setOnLongClickListener(new OnLongClickListenerImpl()); } private class OnLongClickListenerImpl implements OnLongClickListener { public boolean onLongClick(View v) { try { MyLongClickDemo.this.clearWallpaper(); // 清除已有的桌面 MyLongClickDemo.this.setWallpaper(MyLongClickDemo.this.img .getResources().openRawResource(R.drawable.mldn_bg)); // 设置桌面 MyLongClickDemo.this.info.setText("手机桌面背景已修改。"); } catch (Exception e) { e.printStackTrace() ; MyLongClickDemo.this.info.setText("手机桌面背景设置失败。"); } return false; } } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <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>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28