gpt4 book ai didi

android - onClickListener 问题

转载 作者:行者123 更新时间:2023-11-29 17:49:49 25 4
gpt4 key购买 nike

所以我想做一个简单的图像按钮:

<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_image"
android:contentDescription="@string/desc/>

xml:

public class MainActivity extends ActionBarActivity {
protected void onCreate1 (Bundle savedInstaceState){
Bundle savedInstanceState;
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button1 = (Button) findViewById (R.id.button1);

View.setOnClickListener(new onClickListener () {
public void onClick (View v) {
Toast.makeText(v.getContext(), "You clicked it. Genius.", Toast.LENGTH_SHORT).show();
}
});

但是一直报错:"The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new onClickListener(){})"

我导入了android.view.View.OnClickLIstener;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

最佳答案

你应该这样做:

 button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});

使用 button1 并检查 OnClickListener 大小写。

关于android - onClickListener 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23702265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com