私はまだアンドロイドに慣れていないので、できるだけ注意深く、できればサンプルコードで説明してください。私は現在、ボタンをクリックすると新しい事実を表示するだけのファクトファインダーアプリに取り組んでいます。新しいファクトが表示されるたびに、同じ画面が表示されますが、色が異なります。これを行うには、事実を配列に格納し、色番号も配列に格納してから、それらをランダムに一致させました。特定の(色のようにランダムではない)事実がポップアップしたときに、背景をカスタマイズされた画像に変更するにはどうすればよいですか。単純に 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;
}
}