我的 MainActivity 类中有一个 randomJoke 代码,基本上告诉我的应用程序在每次单击按钮时显示一个字符串。在笑话视图中,我试图创建一个函数来做同样的事情,除了笑话视图中的按钮。因此,当用户想要另一个笑话时,他们不必返回主页。相反,他们只需单击笑话视图上的按钮即可。我应该在“onClicK”之后添加什么来告诉应用程序执行与我的 randomJoke 代码相同的功能(在同一个活动类中)。谢谢!!
到目前为止,我的代码是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
setupJokeButton();
}
private void setupJokeButton() {
// TODO Auto-generated method stub
Button JokeButton = (Button) findViewById(R.id.nextjoke);
JokeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
我的 randomJoke 代码如下所示:
public void randomJoke (View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
Random rand = new Random();
int lowerBound = 1, upperBound = 83; //HERE IS WHERE YOU ADD THE HIGHEST NUMBER OF JOKES THERE IS
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);