我还是 android 新手,所以请尽可能仔细地解释,最好使用示例代码。我目前正在开发事实查找器应用程序,该应用程序仅在单击按钮时显示一个新事实。每次显示新事实时,都会弹出相同的屏幕,但颜色不同。我通过将事实存储在数组中以及将颜色编号存储在数组中然后随机匹配它们来做到这一点。当弹出特定(不是随机的颜色)事实时,如何将背景更改为自定义图像。我知道要设置第一张图片的背景,我们可以简单地编写 android:background="@drawable/(name_of_image)" 我该怎么做,这是我到目前为止编写的代码:不同颜色存储在一个名为 ColorWheel 的新类
色轮
package com.example.kharl.funfacts;
import android.graphics.Color;
import java.util.Random;
/**
* Created by Kharl on 1/27/2015.
*/
public class ColorWheel {
//Member variable (propoerties about the object)
public String[] mColors = {
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" // light gray
};
String color="";
//Method (abilities:things the object can do)
public int getColor(){
//Randomly select a fact
Random randomGenerator =new Random(); // construct a new random generator
int randomNumber =randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;
}
}
FunFactsActivity.Java
public class FunFactsActivity extends Activity {
public static final String TAG =FunFactsActivity.class.getSimpleName();
private FactBook mFactBook = new FactBook();
private ColorWheel mColorWheel= new ColorWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Declare our view variables and assign them the views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
final Button showFactButton= (Button) findViewById(R.id.showFactButton);
final RelativeLayout relativelayout =(RelativeLayout) findViewById(R.id.relativeLayout);
View.OnClickListener listener= new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = mFactBook.getFact();
//Update the label with our dynamic fact
factLabel.setText(fact);
int color = mColorWheel.getColor();
relativelayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
}
};
showFactButton.setOnClickListener(listener);
//Toast.makeText(this,"YAY! our activity was created",Toast.LENGTH_LONG).show();
Log.d(TAG, "We are Logging from the oncreate method");
}
}
概况介绍
package com.example.kharl.funfacts;
import java.util.Random;
/**
* Created by Kharl on 1/26/2015.
*/
public class FactBook {
//Member variable (propoerties about the object)
public String[] mFacts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built." };
String fact="";
//Method (abilities:things the object can do)
public String getFact(){
//Randomly select a fact
Random randomGenerator =new Random(); // construct a new random generator
int randomNumber =randomGenerator.nextInt(mFacts.length);
fact = mFacts[randomNumber];
return fact;
}
}