gpt4 book ai didi

java - "Set wallpaper"按钮功能,Android

转载 作者:行者123 更新时间:2023-12-01 16:02:31 26 4
gpt4 key购买 nike

我创建了一个自定义图库;但是,“设置壁纸”按钮不会设置壁纸。这是我的壁纸.java。我只是不知道如何实现 OnClickListener,然后将我的按钮设置为使用 onclicklistener,如下所示:

buttonName.setOnClickListener(this);

package com.totem;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;

import java.io.IOException;
import java.io.InputStream;

public class Wallpaper extends Activity implements
AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {

private static final String LOG_TAG = "Home";

private static final Integer[] THUMB_IDS = {
R.drawable.andy_small,

};

private static final Integer[] IMAGE_IDS = {
R.drawable.andy,

};

private Gallery mGallery;
private boolean mIsWallpaperSet;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.wallpaper_chooser);

mGallery = (Gallery) findViewById(R.id.gallery);
mGallery.setAdapter(new ImageAdapter(this));
mGallery.setOnItemSelectedListener(this);
mGallery.setOnItemClickListener(this);
}

@Override
protected void onResume() {
super.onResume();
mIsWallpaperSet = false;
}

@SuppressWarnings("unchecked")
public void onItemSelected(AdapterView parent, View v, int position, long id) {
getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
}

@SuppressWarnings("unchecked")
public void onItemClick(AdapterView parent, View v, int position, long id) {
selectWallpaper(position);
}

private synchronized void selectWallpaper(int position) {
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
try {
InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
setWallpaper(stream);
setResult(RESULT_OK);
finish();
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to set wallpaper " + e);
}
}

@SuppressWarnings("unchecked")
public void onNothingSelected(AdapterView parent) {
}

public boolean onTouchEvent(MotionEvent event, Object Action_Down) {
getAction(Action_Down);
selectWallpaper(mGallery.getSelectedItemPosition());
return true;
}

public class ImageAdapter extends BaseAdapter {

private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return THUMB_IDS.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);

i.setImageResource(THUMB_IDS[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(android.R.drawable.picture_frame);
return i;
}

}

}使用 GeSHi 1.0.8.4 在 0.242 秒内解析

最佳答案

转到http://www.xtensivearts.com/topics/tutorials/有视频教程可以帮助您使用 onclicklistener

关于java - "Set wallpaper"按钮功能,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3460333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com