0
嘿大家:)我一直在撓我的頭,這似乎找不到合適的答案,即使我確定它的一個簡單的問題。生成按鈕上的隨機整數點擊沒有刷新活動
我有一個程序,當另一個按鈕被按下時,它會在一組按鈕上生成隨機字母文本。 當我打開應用程序時,它加載正常,第一次按下按鈕時它會正確生成,但在此之後,我似乎無法讓它重新生成隨機字母。
我可以通過添加Intent來完成我想要的操作,並且基本上「刷新」(主題活動不確定),但是我想爲按鈕點擊添加一個計數器,並且在活動執行時重置。
{Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
overridePendingTransition(0, 0);
我只是似乎不能繞我的腦海圍繞如何做到這一點,否則。這似乎是我需要每次點擊按鈕時重新運行java。有什麼建議麼?
下面是一些代碼,我使用的是按鈕而不是textview,因爲我a)想設置一個簡單的背景b)可能會讓它們稍後可點擊。非常感謝!
public class MainActivity extends Activity
{
Button spin;
Button reel1,reel2,reel3,reel4;
private String rnd,rnd2,rnd3,rnd4;
int count1=50;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();}
private static String rndm[] = {"A","B","C","D"};
{rnd = rndm[(int) (Math.random() * rndm.length)];}
private static String rndm2[] = {"A","B","C","D"};
{rnd2 = rndm2[(int) (Math.random() * rndm2.length)];}
private static String rndm3[] = {"A","B","C","D"};
{rnd3 = rndm3[(int) (Math.random() * rndm3.length)];}
private static String rndm4[] = {"A","B","C","D"};
{rnd4 = rndm4[(int) (Math.random() * rndm4.length)];}
public void perform_action(View v){}
public void addListenerOnButton() {
spin = (Button) findViewById(R.id.spin);
spin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
{Toast.makeText(getApplicationContext(),"button pressed", Toast.LENGTH_SHORT).show();}
{Button tv = (Button) findViewById(R.id.reel1);
tv.setText(String.valueOf(rnd));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel2);
tv.setText(String.valueOf(rnd2));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel3);
tv.setText(String.valueOf(rnd3));
tv.setTextColor(Color.parseColor("#000000"));}
{Button tv = (Button) findViewById(R.id.reel4);
tv.setText(String.valueOf(rnd4));
tv.setTextColor(Color.parseColor("#000000"));
你可以接受你自己的答案。不需要註冊。 – Acapulco