android换肤项目是本人学习android不到一个月做的第一个项目,所以有很多可以优化的地方,仅给大家提供一个参考。
package wang.sc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GridviewActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPref pe = new SharedPref();
int dra = pe.getpreferencedr(this);
int sty = pe.getpreferencest(this);
LinearLayout lay = (LinearLayout) this.findViewById(R.id.main);
lay.setBackgroundResource(dra);
setTheme(sty);
setContentView(lay);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
Builder dialog = new AlertDialog.Builder(GridviewActivity.this)
.setIcon(R.drawable.appearance)
.setTitle("您希望换主题吗?")
.setPositiveButton(
"是",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dia,
int whichButton) {
LayoutInflater factory = LayoutInflater
.from(GridviewActivity.this);
final View diaView = factory.inflate(
R.layout.dialog, null);
ImageButton button = (ImageButton) diaView
.findViewById(R.id.qingchun);
button
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast
.makeText(
GridviewActivity.this,
"尽情的在青春飞扬世界里畅游吧",
Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent();
intent
.setClass(
GridviewActivity.this,
Qingchun.class);
GridviewActivity.this
.startActivity(intent);
finish();
}
});
ImageButton button1 = (ImageButton) diaView
.findViewById(R.id.huanghun);
button1
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast
.makeText(
GridviewActivity.this,
"尽情的在黄昏静谧世界里畅游吧",
Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent();
intent
.setClass(
GridviewActivity.this,
Huanghun.class);
GridviewActivity.this
.startActivity(intent);
finish();
}
});
ImageButton button2 = (ImageButton) diaView
.findViewById(R.id.jiqing);
button2
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast
.makeText(
GridviewActivity.this,
"尽情的在激情四射世界里畅游吧",
Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent();
intent
.setClass(
GridviewActivity.this,
jiqing.class);
GridviewActivity.this
.startActivity(intent);
finish();
}
});
ImageButton button3 = (ImageButton) diaView
.findViewById(R.id.lvyi);
button3
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast
.makeText(
GridviewActivity.this,
"尽情的在绿意盎然世界里畅游吧",
Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent();
intent
.setClass(
GridviewActivity.this,
lvyi.class);
GridviewActivity.this
.startActivity(intent);
finish();
}
});
ImageButton button4 = (ImageButton) diaView
.findViewById(R.id.weilan);
button4
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast
.makeText(
GridviewActivity.this,
"尽情的在深度蔚蓝世界里畅游吧",
Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent();
intent
.setClass(
GridviewActivity.this,
weilan.class);
GridviewActivity.this
.startActivity(intent);
finish();
}
});
ImageButton button5 = (ImageButton) diaView
.findViewById(R.id.suiji);
button5
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent
.setClass(
GridviewActivity.this,
jindu.class);
GridviewActivity.this
.startActivity(intent);
finish();
}
});
Builder dlg = new AlertDialog.Builder(
GridviewActivity.this).setView(
diaView).setIcon(
R.drawable.jiqing).setTitle(
"选择您喜欢的主题");
dlg.create();
dlg.show();
}
})
.setNegativeButton(
"否",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast
.makeText(
GridviewActivity.this,
"您已经取消主题设置",
Toast.LENGTH_SHORT)
.show();
}
});
dialog.create();
dialog.show();
}
});
}
}
上面这个是主activity 采用dialog imagebutton 相结合
package wang.sc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class Huanghun extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Builder builder = new AlertDialog.Builder(this)
.setMessage("您想保存主题吗?")
.setPositiveButton("保存", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
SharedPref pe = new SharedPref();
pe.setpreference(Huanghun.this,R.drawable.huanghun1,R.style.huanghun);
Intent intent = new Intent();
intent.setClass(Huanghun.this, GridviewActivity.class);
Huanghun.this.startActivity(intent);
}
})
.setNegativeButton("取消",new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Huanghun.this, GridviewActivity.class);
Huanghun.this.startActivity(intent);
}
});
builder.create();
builder.show();
LinearLayout layout = new LinearLayout(Huanghun.this);
layout.setBackgroundResource(R.drawable.huanghun1);
setContentView(layout);
}
}
上面这个是保存选中的主题
下面这个是随机换肤
package wang.sc;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class jindu extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.jindu);
setProgressBarVisibility(true);
ProgressBar pb = (ProgressBar) findViewById(R.id.pb);
setProgress(pb.getProgress()*100);
setSecondaryProgress(pb.getSecondaryProgress()*100);
try{
Thread.sleep(3000);
}
catch(Exception e){
e.printStackTrace();
}
finally
{
}
Random r = new Random();
int s = Math.abs(r.nextInt())%5;
String[]a ={"qingchun","huanghun","weilan","jiqing","lvyi"};
if((a[s].equals(a[0]))){
Toast.makeText(jindu.this, "尽情的在青春飞扬世界里畅游吧",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(jindu.this,Qingchun.class);
jindu.this.startActivity(intent);
finish();
}
if((a[s].equals(a[1]))){
Toast.makeText(jindu.this, "尽情的在黄昏静谧世界里畅游吧",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(jindu.this,Huanghun.class);
jindu.this.startActivity(intent);
finish();
}
if((a[s].equals(a[2]))){
Toast.makeText(jindu.this, "尽情的在深度蔚蓝世界里畅游吧",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(jindu.this,weilan.class);
jindu.this.startActivity(intent);
finish();
}
if((a[s].equals(a[3]))){
Toast.makeText(jindu.this, "尽情的在激情四射世界里畅游吧",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(jindu.this,jiqing.class);
jindu.this.startActivity(intent);
finish();
}
if((a[s].equals(a[4]))){
Toast.makeText(jindu.this, "尽情的在绿意盎然世界里畅游吧",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(jindu.this,lvyi.class);
jindu.this.startActivity(intent);
finish();
}
}
}
下面是sharedpreference
package wang.sc;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPref {
public void setpreference(Activity act, int id, int id2) {
SharedPreferences sp = act.getSharedPreferences("zhuti",
Activity.MODE_PRIVATE);
sp.edit().putInt("dr", id).putInt("st", id2).commit();
}
public int getpreferencedr(Activity act)
{
SharedPreferences sp =
act.getSharedPreferences("zhuti",Context.MODE_WORLD_READABLE);
int dra = sp.getInt("dr", R.drawable.xiangji);
return dra;
}
public int getpreferencest(Activity act)
{
SharedPreferences sp =
act.getSharedPreferences("zhuti",Context.MODE_WORLD_READABLE);
int dra = sp.getInt("st",R.style.qingchun);
return dra;
}
}
配置文件相对简单在此不在赘述。 作者— 河北北方学院 —醉雨蝶