Android: 无法调用返回按钮的buttonOnClick函数

huangapple go评论149阅读模式
英文:

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;
        }
    }
}

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

请查看以下视频并告诉我现在有什么问题?
youtube.com/watch?v=heD9QGKtusY&feature=youtu.be

GeneralActivity布局的完整代码:

<?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" />

</LinearLayout>

现在,我不喜欢这个网站,因为有这种人,他们没有任何原因就设置-1.. 他们就像这些页面上的垃圾。

我用最大的示例和视频解释了我的问题。你还需要看到什么来明确问题吗?白痴。

好吧,让他们活着吧,这是这个网站持有者的糟糕政策。

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

当我实现OnViewListener接口时,我忘记在onClick方法上标记@Override,另一个“大”错误是,处理程序名称onClick我用大写字母O写成了onClick。

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

字母O和o

  • 嗯,你明白了)))

我从事软件开发已经30年了,这些错误是我遇到的最难的错误。就像括号 [ { jr (..

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

英文:

Actually I don't understand why it doesn't see the buttonOnClick listener?

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 = &quot;&quot;;

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

Also,
In GeneralActivitry designer everything is written out - onClick for both buttons and this method referred to the GeneralActivity.

Pls, see the movie and tell me what's wrong now?
youtube.com/watch?v=heD9QGKtusY&feature=youtu.be

FULL CODE OF GENERALACTIVITY LAYOUT:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;LinearLayout
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;fill_parent&quot;
        android:orientation=&quot;vertical&quot;
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

            &lt;EditText
                android:id=&quot;@+id/licence&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;30pt&quot;
                android:ems=&quot;10&quot;
                android:hint=&quot;1SAT&quot;
                android:inputType=&quot;textPersonName&quot;
                android:layout_weight=&quot;0&quot;
                android:singleLine=&quot;true&quot;
                android:text=&quot;SATEST LICENCE NUMBER&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/connect&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;30pt&quot;
        android:layout_weight=&quot;0&quot;
        android:onClick=&quot;buttonOnClick&quot;
        android:text=&quot;CONNECT&quot; /&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/messages&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;fill_parent&quot;
        android:layout_weight=&quot;1&quot;
        android:orientation=&quot;vertical&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/sendall&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;30pt&quot;
        android:layout_weight=&quot;0&quot;
        android:enabled=&quot;false&quot;
        android:onClick=&quot;buttonOnClick&quot;
        android:text=&quot;SEND2ALL&quot; /&gt;

    &lt;/LinearLayout&gt;

=====================================
Well, I don't like this site exactly because of that kind of people who set -1 without any reason for that.. they just like shitters in that pages.
I explained my problem with MAXIMUM example and even VIDEO.. WHAT ELSE do you need to see to get the question clear? Idiots.

Ok, let they live, it is the shit policy of this site holders..

Now you read carefully what was my error:

When I implemented OnViewListener interface I forgot to mark the method onClick with @Override and another "BIG" mistake was that the handler name onClick I wrote from UPPER LETTER OnClick..

Actually that was my main mistake. Before publishing my Q here, I try to understand everything by myself, and I tried all of the suggestions that guys gave me.. but the

> letter O and o

  • hmm you understood )))

I am 30 years in software developing, and these kind of mistakes are the hardest mistakes I have ever met. Same like brackets [ { jr (..

Thank you all for you help.. I'll set the Answer to the first one.. Thank you again.

答案1

得分: 1

Option - 1: 您已经从XML设置了buttononClick监听器,所以无需再从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
        }
    }
}

Option - 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);
    }
}
英文:

Option - 1: You have already set the onClick listener to button from xml, so no need to set it again from java code. Remove those line.

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(&quot;Connect me&quot;);
       
        if (v.getId() == R.id.connect) {
            MessageBox.Show(&quot;Connect me&quot;);
            return;
        } else if(v.getId() == R.id.sendall) {

            //Add your logic here
        }
    }
}

Option - 2: If you want to set the listener from java code, then remove onClick attribute from xml and set it like below from code:

public class GeneralActivity extends AppCompatActivity {

    boolean clicked = false;
    public static String LICENCE = &quot;&quot;;
    
    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);
    }
}

答案2

得分: 1

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); // 调用onClick()方法
        Button two = (Button) findViewById(R.id.twoButton);
        two.setOnClickListener(this);
    }

    // 在你的onCreate()之外

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

            case R.id.oneButton:
                // 执行你的代码
                break;

            case R.id.twoButton:
                // 执行你的代码
                break;

            default:
                break;
        }
    }
}

提示:输入新的View,然后按Ctrl+Spacebar,你将获得自动生成的函数。

英文:

Solution:

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;
    }

            }

TIP: Enter new View. And then press Ctrl+Spacebar you will get the function auto generated.

答案3

得分: 1

你可以使用内置的OnClick方法来完成这个操作:以下是如何做的示例

只是不要忘记在你的Activity中实现View.OnClickListener

public class GeneralActivity extends AppCompatActivity implements View.OnClickListener {
    // 在下面添加或不添加这些行

    @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:
                //做一些事情
                break;
        }
    }
}

请注意:这是Java代码示例,用于在Android应用程序中处理点击事件。

英文:

you can use built it OnClick method to do this for you: here is an example of how to do it

just don't forget to implement View.OnClickListener in your Activity
`

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;
    }
}

`

huangapple
  • 本文由 发表于 2020年1月7日 01:32:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定