有人可以告诉我我在这段代码中出错了,因为我的clicklistener不工作,并且没有出现从0到1的数字。
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 :(得分:0)
尝试更改
View.OnClickListener Listener = new View.OnClickListener() {
到
showFactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// DO STUFF
}
});
答案 1 :(得分:0)
试试这个
Button showFactButton = (Button) findViewById(R.id.showFactButton);
showFactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Your Code
}});