我正在尝试使用Toast内部OnCLickListener.我的代码触发以下错误:
The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText name = (EditText)findViewById(R.id.name);
String Lname = name.getText().toString();
Toast.makeText(this, Lname, Toast.LENGTH_SHORT).show();
}
});
Run Code Online (Sandbox Code Playgroud)
Com*_*ish 94
正如肯尼所说,this是指View.OnClickListener而不是你的Activity.改变这个,到MyActivity.this.
例如,
public class MyActivity extends Activity {
// ... other code here
Toast.makeText(MyActivity.this, Lname, Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30414 次 |
| 最近记录: |