您的位置:首页 > 其它

setOnLongClickListener setWallPaper

2014-06-12 23:54 246 查看
 

 

.java文件

package com.example.setwallpaper_e;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
private ImageView iv=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) this.findViewById(R.id.iv_info);
MainActivity.this.iv.setOnLongClickListener(new OnLongClickListener() {

public boolean onLongClick(View v) {
// TODO Auto-generated method stub
try {
MainActivity.this.clearWallpaper();

MainActivity.this.setWallpaper(MainActivity.this.iv.getResources().openRawResource(R.drawable.beauty_2));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

.xml文件

<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"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/iv_info"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/beauty"
/>

</LinearLayout>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: