我在 Android 中的 toast 有一个小问题:/
我想在 button1
被激活时创建一个小 toast
这是我的代码:
MainActivity.java
package com.andruiden.toast;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.andruiden.toast.R;
public class MainActivity extends Activity {
//Klick Listener
class MeinClickListener implements OnClickListener{
public void onClick(View v){
String text = "Es wurde geklickt";
Toast t = Toast.makeText(v.getContext(), text, Toast.LENGTH_SHORT);
t.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button beim Hörer registrieren
MeinClickListener t = new MeinClickListener();
Button test = (Button) findViewById(R.id.button1);
test.setOnClickListener(t);
}
}
在线 test.setOnClickListener(t);
我收到错误:
The method setOnClickListener(View.OnClickListener) in the type view is not applicable to the arguments
这是什么意思?
我可以在这里将代码更改为:
test.setOnClickListener((android.view.View.OnClickListener) t);
但是当我在 Galaxy s2 上运行 .apk 时,什么也没发生..:/
抱歉我的英语不好xD
最佳答案
请从导入部分删除这些行:
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
这些是对话框界面
并使用它:
import android.view.View.OnClickListener;
你的类(class)应该是:
class MeinClickListener implements OnClickListener
{
public void onClick(View v)
{
String text = "Es wurde geklickt";
Toast t = Toast.makeText(v.getContext(), text, Toast.LENGTH_SHORT);
t.show();
}
}
关于java - Android setOnClickListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18987691/