首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击程序中的活动和非活动按钮后,如何更改文本视图?

单击程序中的活动和非活动按钮后,如何更改文本视图?
EN

Stack Overflow用户
提问于 2018-06-01 20:02:36
回答 2查看 55关注 0票数 0

我有两个名为"Active“和"Deactive”的按钮和一个TextView。当我点击“激活”按钮时,它必须向电话号码发送包含“CMDSAD_ON”的短消息,并将"Enable“设置为我的TextView,当单击"Deactive”按钮时,它必须发送"CMD_OFF“并将"Disable”设置为相同的TextView。

但当我运行它时,它只将“禁用”设置为文本视图,并且当我单击按钮时它不会改变,甚至在发送短信之前设置它。我不知道怎样才能解决它的问题。谢谢

代码语言:javascript
复制
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //intent to filter for SS message received.
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");

       // Button2 is the id of "Active button" and Button4 is the id   of "Deactive button".

        Button button1 = (Button) findViewById(R.id.button2);
        button1.setOnClickListener(this);
        Button button2 = (Button) findViewById(R.id.button4);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.button2:

                String mymsg = "CMDSAD_ON";
                String thenumber = "916722";
                sendMsg (thenumber, mymsg);


            case R.id.button4:
                String msg = "CMDSAD_OFF";
                String phonenumber = "916722";
                sendNackMsg(phonenumber, msg);

            default:
            }}

和sendNackMsg函数类似于sendMsg,除了最后一个链接将文本视图更改为"disable"( -> Textstatus.setText("disable");)

代码语言:javascript
复制
public void sendMsg (String thenumber, String mymsg){
        String SENT = "Message sent";
        String DELIVERED = "Message delivered";


        SmsManager smsManager = SmsManager.getDefault();

        Context curContext = this.getApplicationContext();

        PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("SENT"), 0);

        curContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Sent.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
                                Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }, new IntentFilter("SENT"));

        PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("DELIVERED"), 0);

        curContext.registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode()) {
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "Delivered.",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case Activity.RESULT_CANCELED:
                                Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
                                        Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                }, new IntentFilter("DELIVERED"));

        PackageManager pm = this.getPackageManager();

        if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
                !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
            Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
        } else{
            smsManager.sendTextMessage("9167227450", null, "CMDSAD_ON_1234", sentPending, deliveredPending);
        setContentView(R.layout.activity_main);
        TextView Textstatus = (TextView) findViewById(R.id.Textstatus);
        Textstatus.setText("enable");}
    }


public void sendNackMsg(String phonenumber, String msg) {
        String SENT = "Message sent";
        String DELIVERED = "Message delivered";


        SmsManager smsManager = SmsManager.getDefault();

        Context curContext = this.getApplicationContext();

        PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("SENT"), 0);

        curContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Sent.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
                                Toast.LENGTH_LONG).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
                                Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }, new IntentFilter("SENT"));

        PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
                0, new Intent("DELIVERED"), 0);

        curContext.registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode()) {
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "Delivered.",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case Activity.RESULT_CANCELED:
                                Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
                                        Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                }, new IntentFilter("DELIVERED"));

        PackageManager pm = this.getPackageManager();

        if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
                !pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
            Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
        } else{
            smsManager.sendTextMessage("09167227450", null, "CMDSAD_OFF_1234", sentPending, deliveredPending);
            setContentView(R.layout.activity_main);
            TextView Textstatus = (TextView) findViewById(R.id.Textstatus);
            Textstatus.setText(disable);}
    }
EN

回答 2

Stack Overflow用户

发布于 2018-06-01 20:24:58

在这里你正在设置一个固定的文本在两个按钮上点击

在您的buttonOnCLick上使用此条件,并在所有按钮单击后应用break;语句。

在单击两个按钮时使用与这些条件相同的条件,然后更改此条件

代码语言:javascript
复制
 String mymsg = "CMDSAD_ON";
    String thenumber = "916722";
    sendMsg (thenumber, mymsg);

在您的button4上单击

代码语言:javascript
复制
       String msg = "CMDSAD_OFF";
            String phonenumber = "916722";
            sendNackMsg(phonenumber, msg);

像以前一样替换您的消息代码,在这两种情况下,这是针对button2的,单击

代码语言:javascript
复制
    if(myTargetButton.getText().toString.equalsIgnoreCase("active")){

    myTargetButton.setText("Disable")
    String mymsg = "CMDSAD_ON";
    String thenumber = "916722";
    sendMsg (thenumber, mymsg);

    }else if(myTargetButton.getText().toString.equalsIgnoreCase("Disable")){

    myTargetButton.setText("Enable")
    String mymsg = "CMDSAD_ON";
    String thenumber = "916722";
    sendMsg (thenumber, mymsg);

    }



 TextView Textstatus = (TextView) findViewById(R.id.Textstatus);
    Textstatus.setText("فعال");}

更改它并使用您的方法中的文本,如下所示:

代码语言:javascript
复制
TextView Textstatus = (TextView) findViewById(R.id.Textstatus);
        Textstatus.setText(mymsg);}

希望它能为你工作。

票数 0
EN

Stack Overflow用户

发布于 2018-06-01 20:52:51

您未在onClick上应用break语句。这就是为什么它总是抛出你的最后一个案例并将disable设置为你的TextView。请检查以下内容:

代码语言:javascript
复制
 @Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.button2:

            String mymsg = "CMDSAD_ON";
            String thenumber = "916722";
            sendMsg (thenumber, mymsg);
            break;

        case R.id.button4:
            String msg = "CMDSAD_OFF";
            String phonenumber = "916722";
            sendNackMsg(phonenumber, msg);
            break;
        default: 
            break;
        }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50643004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档