我正在制作一个简单的笑话应用程序。基本上我用字符串来保存每一个笑话。所以我在应用程序中按笑话,它给了我一个笑话,然后我按回来,我再次按笑话,它给了我笑话#2,依此类推。我的问题是所有笑话都有白色背景。我已经尝试android:background
在我的所有xml布局文件中使用,但我仍然得到白色背景。我认为这与我使用字符串来显示笑话的事实有关。这是我在.java
课堂上用来开玩笑的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void randomJoke (View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
//Random rand = new Random();
//int lowerBound = 1, upperBound = 10;
//int randomNumber = rand.nextInt(upperBound - lowerBound + 1) + lowerBound;
//String jokeNumber = "joke" + String.valueOf(randomNumber);
//String mess = getResources().getString(getStringResourcePath(getApplicationContext(),jokeNumber));
String jokeNumber = "joke" + String.valueOf(iteration);
iteration += 1;
String mess = getResources().getString(getStringResourcePath(getApplicationContext(), jokeNumber));
intent.putExtra(EXTRA_MESSAGE, mess);
startActivity(intent);
}
public int getStringResourcePath(Context context, String name) {
int joke = context.getResources().getIdentifier(name, "string", "com.primarycode.punnyjokes");
return joke;
}
}