Android: 无法回调 buttonOnCklick

Android: Unable to call back buttonOnCklick

其实我不明白为什么它没有看到 buttonOnClick 侦听器?

public class GeneralActivity extends AppCompatActivity {
    // with or without these lines below 
    private View.OnClickListener buttonOnClick = this.buttonOnClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalData.getInstance().gContext = this;
        setContentView(R.layout.activity_general);
        ((TextView) findViewById(R.id.licence)).setText(MySettings.Values().LicenceNo);
        Button connect = (Button)findViewById(R.id.connect);
        connect.setOnClickListener(buttonOnClick);
        Button send = (Button)findViewById(R.id.sendall);
        send.setOnClickListener(buttonOnClick);
    }

    boolean clicked = false;
    public static String LICENCE = "";

    public void buttonOnClick(View v) {
        MessageBox.Show("Connect me");
        Button b = (Button) v;
        if (b.getId() == R.id.connect) {
            MessageBox.Show("Connect me");
                return;

此外, 在 GeneralActivitry 设计器中,所有内容都已写出 - 两个按钮的 onClick 和此方法都引用了 GeneralActivity。

请看电影并告诉我现在有什么问题? youtube.com/watch?v=heD9QGKtusY&feature=youtu.be

通用布局的完整代码:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

            <EditText
                android:id="@+id/licence"
                android:layout_width="match_parent"
                android:layout_height="30pt"
                android:ems="10"
                android:hint="1SAT"
                android:inputType="textPersonName"
                android:layout_weight="0"
                android:singleLine="true"
                android:text="SATEST LICENCE NUMBER" />

    <Button
        android:id="@+id/connect"
        android:layout_width="match_parent"
        android:layout_height="30pt"
        android:layout_weight="0"
        android:onClick="buttonOnClick"
        android:text="CONNECT" />

    <LinearLayout
        android:id="@+id/messages"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" />

    <Button
        android:id="@+id/sendall"
        android:layout_width="match_parent"
        android:layout_height="30pt"
        android:layout_weight="0"
        android:enabled="false"
        android:onClick="buttonOnClick"
        android:text="SEND2ALL" />

好吧,我不喜欢这个网站正是因为那些人​​无缘无故地设置了 -1..他们只是喜欢那些页面中的垃圾。 我用 MAXIMUM 示例甚至视频解释了我的问题。您还需要看什么才能弄清楚问题?白痴。

好吧,让他们活下去吧,这就是网站持有者的狗屁政策..

现在你仔细看看我的错误是什么:

当我实现 OnViewListener 接口时,我忘记用 @Override 标记 onClick 方法,另一个 "BIG" 错误是我从大写字母写的处理程序名称 onClick O n点击..

实际上那是我的主要错误。在此处发布我的问题之前,我尝试自己理解所有内容,并且尝试了大家给我的所有建议..但是

letter O and o

我从事软件开发已有 30 年,这些错误是我遇到过的最严重的错误。与括号 [ { jr (..

谢谢大家的帮助..我把答案设为第一个..再次感谢.

选项 - 1: 您已经将 onClick 侦听器从 xml 设置为 button,因此无需再次设置来自 java 代码。删除那些行。

public class GeneralActivity extends AppCompatActivity {
    // with or without these lines below 
    //private View.OnClickListener buttonOnClick = this.buttonOnClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ....

        //Button connect = (Button) findViewById(R.id.connect);
        //connect.setOnClickListener(buttonOnClick);
        //Button send = (Button) findViewById(R.id.sendall);
        //send.setOnClickListener(buttonOnClick);
    }

    public void buttonOnClick(View v) {
        MessageBox.Show("Connect me");

        if (v.getId() == R.id.connect) {
            MessageBox.Show("Connect me");
            return;
        } else if(v.getId() == R.id.sendall) {

            //Add your logic here
        }
    }
}

选项 - 2: 如果你想从 java 代码中设置监听器,那么从 xml 中删除 onClick 属性并设置它就像下面的代码:

public class GeneralActivity extends AppCompatActivity {

    boolean clicked = false;
    public static String LICENCE = "";

    private View.OnClickListener buttonOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.connect:
                    // Your connect logic here
                    break;
                case R.id.sendall:
                    // Your send all logic here
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalData.getInstance().gContext = this;
        setContentView(R.layout.activity_general);
        ((TextView) findViewById(R.id.licence)).setText(MySettings.Values().LicenceNo);
        Button connect = (Button) findViewById(R.id.connect);
        Button send = (Button) findViewById(R.id.sendall);


        connect.setOnClickListener(buttonOnClick);
        send.setOnClickListener(buttonOnClick);
    }
}

解法:

public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);

    Button one = (Button) findViewById(R.id.oneButton);
    one.setOnClickListener(this); // calling onClick() method
    Button two = (Button) findViewById(R.id.twoButton);
    two.setOnClickListener(this);

}
 //outside of your oncreate()

         public void onClick(View v) {
            switch (v.getId()) {

        case R.id.oneButton:
            // do your code
            break;

        case R.id.twoButton:
            // do your code
            break;

       default:
            break;
    }

            }

提示:进入新视图。然后按Ctrl+空格键你会得到自动生成的函数。

您可以使用内置的 OnClick 方法为您执行此操作:这是一个如何执行此操作的示例

只是不要忘记在您的 Activity 中实施 View.OnClickListener `

public class GeneralActivity extends AppCompatActivity implements View.OnClickListener {
// with or without these lines below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GlobalData.getInstance().gContext = this;
    setContentView(R.layout.activity_general);
    ((TextView) findViewById(R.id.licence)).setText(MySettings.Values().LicenceNo);
    Button connect = findViewById(R.id.connect);
    connect.setOnClickListener(this);
    Button send = findViewById(R.id.sendall);
    send.setOnClickListener(this);
}


@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.connect:
            //TODO
            break;
        case R.id.sendall:
            //DO something
            break;
    }
}

`