2015-10-18 89 views
0

有人可以告訴我在這段代碼中我錯了什麼,因爲我在clicklistener上不工作,並且從0到1的數字沒有出現。設置一個OnClickListener

public class FunFactsActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fun_facts); 
    Button showFactButton = (Button)findViewById(R.id.showFactButton); 

    button.setOnClickListener(
     new Button.OnClickListener() { 
      public void onClick(View v) { 
      TextView factTextView = 
        (TextView)findViewById(R.id.factTextView); 
      factTextView.setText("button clicked "); 



      } 
     } 
    ); 

    //declare are View variables and assign the views from the layout file 
    final TextView factLabel = (TextView) findViewById(R.id.factTextView); 



    View.OnClickListener Listener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     //the button was clicked, so update the fact label with a new fact 
      String fact = ""; 
      // randomly select a fact 
      Random randomGenerator = new Random();//construct a new random generator 
      int randomNumber = randomGenerator.nextInt(3); 
+0

仍然停留我將上傳代碼加載,這是現在混亂 – user1727257

回答

0

試圖改變

View.OnClickListener Listener = new View.OnClickListener() { 

,要

showFactButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // DO STUFF 
     } 
    }); 
0

試試這個

Button showFactButton = (Button) findViewById(R.id.showFactButton); 
    showFactButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

//Your Code 
}});