英文:
onClickListener trouble
问题
以下是翻译好的内容:
因此,我正在使用 Android AIDE 在我的手机上学习并开发一个非常小的应用程序,我有如下的代码:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity {
EditText edit1;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit1 = (EditText) findViewById(R.id.edit1);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
//问题(this)来自AIDE错误信息:方法
//'android.view.View.setOnClickListener(android.view.View.OnClickListener)' 不能应用于 '(com.mycompany.myapp.MainActivity)' //
}
public void OnClick(View v) {
switch (v.getId()){
case R.id.btn1:
Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
问题是什么?
英文:
So, I'm learning and developing a very small app with Android AIDE on my phone, I have code like this:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity {
EditText edit1;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit1 = (EditText) findViewById(R.id.edit1);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
//Problem (this) from AIDE error info: Method
'android.view.View.setOnClickListener(android.view.View.OnClickListener)' in 'android.widget.Button' can not be applied to '(com.mycompany.myapp.MainActivity)'//
}
public void OnClick(View v) {
switch (v.getId()){
case R.id.btn1:
Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
What is the problem?
答案1
得分: 4
你的活动必须实现View.OnClickListener接口。
public class MainActivity extends Activity implements View.OnClickListener {
@Override
public void onClick(View view) {}
}
英文:
Your activity have to implement View.OnClickListener interface
public class MainActivity extends Activity implements View.OnClickListener {
@Override
public void onClick(View view) {}
}
答案2
得分: 2
你知道 this
指的是什么,对吗?那是对你的活动的引用,但是 setOnClickListener() 接受的是一个类型为 View.OnClickListener 的参数,而不是你的活动。
看一下来自 官方文档 的示例代码:
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 在用户按下按钮后,此处的代码在主线程上执行
}
});
当你使用 new View.OnClickListener()
创建匿名内部类时,该匿名类实现了 OnClick
接口。
如果你只在你的 MainActivity 上实现 View.OnClickListener,你将不得不改变处理事件的方式,我推测你想尽量减少重构的数量。
英文:
You know what this
refers to, don't you? That's a reference for your activity, but setOnClickListener() takes an argument of type View.OnClickListener, not your activity.
Look at the sample code from the official doc:
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
When you create the anonymous inner class with new View.OnClickListener()
, that anonymous class implements the OnClick
interface.
If you just implement View.OnClickListener on your MainActivity, you'd have to change the way you handle events, and I presume you want to minimize the amount of refactoring.
答案3
得分: 0
这段代码是可以工作的(经过测试)
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener {
EditText edit12;
Button btn12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit12 = (EditText) findViewById(R.id.edit1);
btn12 = (Button) findViewById(R.id.btn1);
btn12.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn12:
Toast.makeText(getApplicationContext(), edit1.getText().toString(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
英文:
This Code is working (Tested)
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener{
EditText edit12;
Button btn12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit12 = (EditText) findViewById(R.id.edit1);
btn12 = (Button) findViewById(R.id.btn1);
btn12.setOnClickListener();
}
public void onClick(View v){
switch (v.getId()){
case R.id.btn12:
Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论