我正在尝试向我的主要活动添加一个点击监听器和一个长时间点击监听器但它没有解雇..下面是我的代码,不知道为什么它不会解雇,有人能告诉我我缺少什么吗?甚至可以为整个活动添加一个监听器吗?
public class HomeFavesActivity extends Activity implements OnClickListener, OnLongClickListener{
private static final String TAG = "HomeFavesCatovoty";
private ArrayList<Integer> mIcons = new ArrayList<Integer>();
IconTray iconTray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "CREATED");
DataBaseManager db = new DataBaseManager(this);
/*db.getWritableDatabase();
db.AddHomeScreenIcon(1);
db.AddHomeScreenIcon(2);
db.AddHomeScreenIcon(3);
db.AddHomeScreenIcon(4);
db.AddHomeScreenIcon(5);
db.AddHomeScreenIcon(6);
db.AddHomeScreenIcon(7);*/
getScreenIcons(db);
}
private void getScreenIcons(DataBaseManager db){
mIcons = db.getScreenIcons("TestScreenIcons");
Log.v(TAG, "List Length:"+ mIcons.size());
RelativeLayout rl = (RelativeLayout)findViewById(R.id.main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.heightPixels;
int screenHeight = metrics.widthPixels;
iconTray = new IconTray(this, mIcons, null, screenWidth, screenHeight);
/*
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
params.addRule(rl.CENTER_IN_PARENT);
iconTray.setLayoutParams(params);*/
rl.addView(iconTray);
rl.setOnLongClickListener(this);
rl.setOnClickListener(this);
}
public void onClick() {
// TODO Auto-generated method stub
Toast.makeText(this, "woot",
Toast.LENGTH_SHORT).show();
}
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
iconTray.longClickMode();
return false;
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
iconTray.clickMode();
}
}
好的,现在LongClick Listener可以正常工作,但是如果添加了点击行,它将不会发布,因为它会生成错误..
类型View中的方法setOnClickListener(View.OnClickListener)不适用于参数(HomeFavesActivity)
答案 0 :(得分:1)
您需要将视图与onclick侦听器关联。
离。
Button button = (Button)findViewById(R.id.my_button);
button.setOnClickListener(this);
然后,无论何时单击按钮,它都会调用您实现的侦听器。 如果您希望可以选择整个活动,可以选择父布局(如main.xml文件中的LinearLayout或RelativeLayout),并遵循上面相同的代码,但是对于该类型的视图。 像这样:
LinearLayout parent = (LinearLayout)findViewById(R.id.parent_layout);
parent.setOnClickListener(this);