我正在编写一个有一些壁纸的应用程序,我希望在应用壁纸时显示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);
}
}
答案 0 :(得分:0)
在每个案例中添加此Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();
。这样就可以了。
答案 1 :(得分:0)
行。显然,选择更新的应用壁纸更新方法并将吐司放入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也可以更快地应用壁纸,而不会偶尔崩溃应用程序。谢谢你的帮助!