2010-08-11 82 views
0

我創建了一個自定義畫廊;但是,「設置壁紙」按鈕不會設置壁紙。這是我擁有的wallpaper.java。我只是迷失在如何實現OnClickListener,然後設置我的按鈕使用像這樣的onclicklistener:「設置壁紙」按鈕功能,Android

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; 
    } 

} 

} 在0.242秒分析時,使用稱作GeSHi 1.0.8.4

回答

0

你在androidmanifest文件中設置用戶權限設置壁紙?

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