0

我正在编写一个包含一些壁纸的应用程序,并且我希望在应用壁纸时显示一个 Toast。目前它仅在您应用第一张壁纸时显示。关于如何让它出现在每张壁纸上的任何想法?

public class WallpaperActivity extends Activity implements OnClickListener {

ImageView display;
int toPhone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wallpaper);
    // Show the Up button in the action bar.
    setupActionBar();

    toPhone = R.drawable.wallpaper_11;

    display = (ImageView) findViewById(R.id.IVdisplay);
    ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
    ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
    ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
    ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
    ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
    ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
    ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
    ImageView image8 = (ImageView) findViewById(R.id.IVimage8);
    ImageView image9 = (ImageView) findViewById(R.id.IVimage9);
    ImageView image10 = (ImageView) findViewById(R.id.IVimage10);
    ImageView image11 = (ImageView) findViewById(R.id.IVimage11);

    Button setWall = (Button) findViewById(R.id.BsetWallpaper);

    image1.setOnClickListener(this);
    image2.setOnClickListener(this);
    image3.setOnClickListener(this);
    image4.setOnClickListener(this);
    image5.setOnClickListener(this);
    image6.setOnClickListener(this);
    image7.setOnClickListener(this);
    image8.setOnClickListener(this);
    image9.setOnClickListener(this);
    image10.setOnClickListener(this);
    image11.setOnClickListener(this);

    setWall.setOnClickListener(this);

}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()){
    case R.id.IVimage1:
        display.setImageResource(R.drawable.wallpaper_1p);
        toPhone = R.drawable.wallpaper_1;
        break;
    case R.id.IVimage2:
        display.setImageResource(R.drawable.wallpaper_2p);
        toPhone = R.drawable.wallpaper_2;
        break;
    case R.id.IVimage3:
        display.setImageResource(R.drawable.wallpaper_3p);
        toPhone = R.drawable.wallpaper_3;
        break;
    case R.id.IVimage4:
        display.setImageResource(R.drawable.wallpaper_4p);
        toPhone = R.drawable.wallpaper_4;
        break;
    case R.id.IVimage5:
        display.setImageResource(R.drawable.wallpaper_5p);
        toPhone = R.drawable.wallpaper_5;
        break;
    case R.id.IVimage6:
        display.setImageResource(R.drawable.wallpaper_6p);
        toPhone = R.drawable.wallpaper_6;
        break;
    case R.id.IVimage7:
        display.setImageResource(R.drawable.wallpaper_7p);
        toPhone = R.drawable.wallpaper_7;
        break;
    case R.id.IVimage8:
        display.setImageResource(R.drawable.wallpaper_8p);
        toPhone = R.drawable.wallpaper_8;
        break;
    case R.id.IVimage9:
        display.setImageResource(R.drawable.wallpaper_9p);
        toPhone = R.drawable.wallpaper_9;
        break;
    case R.id.IVimage10:
        display.setImageResource(R.drawable.wallpaper_10p);
        toPhone = R.drawable.wallpaper_10;
        break;
    case R.id.IVimage11:
        display.setImageResource(R.drawable.wallpaper_11p);
        toPhone = R.drawable.wallpaper_11;
        break;

    case R.id.BsetWallpaper:
        InputStream is = getResources().openRawResource(toPhone);
        Bitmap b = BitmapFactory.decodeStream(is);
        Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();
        try{
            getApplicationContext().setWallpaper(b);
        }catch(IOException e){
            e.printStackTrace();
        }
        break;

    }

}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.wallpaper, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}


}
4

2 回答 2

0

将此添加Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();到您的每个案例中。那会成功的。

于 2013-10-16T01:51:36.113 回答
0

好的。显然,选择更新的应用壁纸并将 Toast 放入 try 方法中的方法每次都会给我一个 Toast:

case R.id.BsetWallpaper:

        try{

            WallpaperManager.getInstance(getApplicationContext()).setResource(toPhone);
            Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();

        }catch(IOException e){
            e.printStackTrace();
        }
        break;

使用 WallpaperManager 还可以更快地应用壁纸,而不会偶尔使应用程序崩溃。谢谢你们的帮助!

于 2013-10-16T14:05:23.173 回答