Android小程序(一个壁纸更换器)

strings.xml

 <string name="txtMsg">选择你喜欢的壁纸,并长按此图</string>
    <string name="msg">选择你喜欢的壁纸</string>
   
   <string-array name="wallPaperItems">
      
       <item>hello1</item>
        <item>hello2</item>
         <item>hello3</item>
          <item>hello4</item>
           <item>hello5</item>
            <item>hello6</item>
             <item>hello7</item>
              <item>hello8</item>
               <item>hello9</item>
                <item>hello10</item>
                 <item>hello11</item>
                  <item>hello12</item>
                   <item>hello13</item>
                  
                   <item>mm1</item>
                   <item>mm2</item>
                   <item>mm3</item>
                   <item>mm4</item>
                   <item>mm5</item>
                   <item>mm6</item>
                   <item>mm7</item>
                   <item>mm8</item>
                   <item>mm9</item>
                   <item>mm10</item>
   </string-array>

main_activity.xml


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       
        android:orientation="vertical"
        >
        <TextView
           android:id="@+id/txtMsg"
           android:text="@string/txtMsg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
        <Spinner
            android:id="@+id/sp"
            android:prompt="@string/msg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/wallPaperItems"
            />
        <ImageView
            android:id="@+id/img"
            android:src="@drawable/hello1"
              android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
       

    </LinearLayout>

MainActivity.java(方法真心觉得有点笨,呵呵 可以复制粘贴,只是为了说明方法)

package com.example.wall;

 

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.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {
    private Spinner sp=null;
    private TextView txtMsg=null;
    private ImageView img=null;
  
   
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        sp=(Spinner)findViewById(R.id.sp);
        txtMsg=(TextView)findViewById(R.id.txtMsg);
        img=(ImageView)findViewById(R.id.img);
        sp.setOnItemSelectedListener(new OnItemSelectedListenerImp1());
        img.setOnLongClickListener(new View.OnLongClickListener() {
      
   public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
          String value=sp.getSelectedItem().toString();
    try {
     MainActivity.this.clearWallpaper();
     selectWall(value);
        txtMsg.setText("桌面修改成功");
    } catch (Exception e) {
     // TODO Auto-generated catch block
     txtMsg.setText("桌面修改失败");
     e.printStackTrace();
     
    }
    
    return false;
   }
  });
 
     
    }
   
    private class OnItemSelectedListenerImp1 implements OnItemSelectedListener
    {

  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   String value=arg0.getItemAtPosition(arg2).toString();
      match(value);
      txtMsg.setText(R.string.txtMsg);
     
     
  }

  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub
   
  }
     
  public void match(String value)
  {
  
    if(value.equalsIgnoreCase("hello1"))
    {
     img.setImageResource(R.drawable.hello1);
    }
    if(value.equalsIgnoreCase("hello2"))
    {
     img.setImageResource(R.drawable.hello2);
    }
    if(value.equalsIgnoreCase("hello3"))
    {
     img.setImageResource(R.drawable.hello3);
    }
    if(value.equalsIgnoreCase("hello4"))
    {
     img.setImageResource(R.drawable.hello4);
    }
    if(value.equalsIgnoreCase("hello5"))
    {
     img.setImageResource(R.drawable.hello5);
    }
    if(value.equalsIgnoreCase("hello6"))
    {
     img.setImageResource(R.drawable.hello6);
    }
    if(value.equalsIgnoreCase("hello7"))
    {
     img.setImageResource(R.drawable.hello7);
    }
    if(value.equalsIgnoreCase("hello8"))
    {
     img.setImageResource(R.drawable.hello8);
    }
    if(value.equalsIgnoreCase("hello9"))
    {
     img.setImageResource(R.drawable.hello9);
    }
    if(value.equalsIgnoreCase("hello13"))
    {
     img.setImageResource(R.drawable.hello13);
    }
    if(value.equalsIgnoreCase("hello10"))
    {
     img.setImageResource(R.drawable.hello10);
    }
    if(value.equalsIgnoreCase("hello11"))
    {
     img.setImageResource(R.drawable.hello11);
    }
    if(value.equalsIgnoreCase("hello12"))
    {
     img.setImageResource(R.drawable.hello12);
    }
    if(value.equalsIgnoreCase("mm1"))
    {
     img.setImageResource(R.drawable.mm1);
    }
   
    if(value.equalsIgnoreCase("mm2"))
    {
     img.setImageResource(R.drawable.mm2);
    }
   
    if(value.equalsIgnoreCase("mm3"))
    {
     img.setImageResource(R.drawable.mm3);
    }
   
    if(value.equalsIgnoreCase("mm4"))
    {
     img.setImageResource(R.drawable.mm4);
    }
   
    if(value.equalsIgnoreCase("mm5"))
    {
     img.setImageResource(R.drawable.mm5);
    }
   
    if(value.equalsIgnoreCase("mm6"))
    {
     img.setImageResource(R.drawable.mm6);
    }
   
    if(value.equalsIgnoreCase("mm7"))
    {
     img.setImageResource(R.drawable.mm7);
    }
   
    if(value.equalsIgnoreCase("mm8"))
    {
     img.setImageResource(R.drawable.mm8);
    }
   
    if(value.equalsIgnoreCase("mm9"))
    {
     img.setImageResource(R.drawable.mm9);
    }
   
    if(value.equalsIgnoreCase("mm10"))
    {
     img.setImageResource(R.drawable.mm10);
    }
   
  }
    }
    
       
 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public void selectWall(String value)
    {
     if(value.equalsIgnoreCase("hello1"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello1));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello2"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello2));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello3"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello3));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello4"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello4));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello5"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello5));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello6"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello6));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello7"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello7));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello8"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello8));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello9"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello9));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello10"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello10));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello11"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello11));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello12"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello12));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("hello13"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.hello13));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm1"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm1));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm2"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm2));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm3"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm3));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm4"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm4));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm5"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm5));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm6"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm6));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm7"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm7));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm8"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm8));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm9"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm9));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
     else if(value.equalsIgnoreCase("mm10"))
     {
      try {
    MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.mm10));
   } catch (NotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
     }
    }
}

效果图:

转载于:https://www.cnblogs.com/wangheblog/archive/2012/11/11/2765148.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值