我正在尝试在用户点击“播放”按钮时显示Toast。但是makeText
在我的代码中是红色的,它不会运行。我哪里错了?
这是Java代码:
// Find the View that shows the play_a button
ImageView play_a = findViewById(R.id.play_a);
// Set a click listener on that View
play_a.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when play_a is clicked on.
Toast.makeText(this, "Sorry, Play doesn't work", Toast.LENGTH_LONG).show();
});
我需要覆盖setOnClickListener
的问题吗?
答案 0 :(得分:2)
OnClickListener
关键字指的是Context
类型的 anonymous-inner-class 。您需要更改它以传递play_a.getContext()
。
例如:extern crate serde;
extern crate serde_json;
fn main() {
let the_file = r#"{
"FirstName": "John",
"LastName": "Doe",
"Age": 43,
"Address": {
"Street": "Downing Street 10",
"City": "London",
"Country": "Great Britain"
},
"PhoneNumbers": [
"+44 1234567",
"+44 2345678"
]
}"#;
let json: serde_json::Value =
serde_json::from_str(the_file).expect("JSON was not well-formatted");
}
或您想要推介的方式。
答案 1 :(得分:1)
您必须覆盖onClick
功能。
play_a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Sorry, Play doesn't work", Toast.LENGTH_LONG).show();
}
});