如何解决重复的if语句问题。

huangapple 未分类评论49阅读模式
英文:

how can I fix the problem of repetitive if statement

问题

我需要有人帮助我创建一个函数,其中将包含所有的条件语句,我尝试使用 switch 但长度仍然太长。
谢谢帮助,我的代码如下所示。
我读取一个 Nfc 标签,然后处理内容。
代码能够正常运行,但因为太长而难以更改,我希望尽可能简化它。
由于我目前的 Java 水平还不太高,所以自己无法做到,如果有人有能力帮忙,我会非常感谢。

private void readFromIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs = null;
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }
        buildTagViews(msgs);
    }
}

private void buildTagViews(NdefMessage[] msgs) {
    if (msgs == null || msgs.length == 0) return;

    String text = "";
    // ...(以下代码省略)
    // 你的函数内容...
    // ...
}

注意:上述翻译中的代码部分仅为你提供的代码内容的翻译,不包括其他问题或回答。如果你需要关于代码的进一步帮助,请随时提问。

英文:

I want someone to help me to create a function that will include all my if statements, I tried to use switch but it still the same too long.
Thanks for help, my code is below
I read an Nfc tag and then I treat content.
The code works well but it's difficult to change because it's too long, I want to simplify it the most possible.
My java level is not to high yet that's why i cannot do it by my self, if somebody has the competence I will be thankfull

private void readFromIntent(Intent intent) {
        String action = intent.getAction();
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            NdefMessage[] msgs = null;
            if (rawMsgs != null) {
                msgs = new NdefMessage[rawMsgs.length];
                for (int i = 0; i &lt; rawMsgs.length; i++) {
                    msgs[i] = (NdefMessage) rawMsgs[i];
                }
            }
            buildTagViews(msgs);
        }
    }
    private void buildTagViews(NdefMessage[] msgs) {
        if (msgs == null || msgs.length == 0) return;

        String text = &quot;&quot;;
//        String tagId = new String(msgs[0].getRecords()[0].getType());
        byte[] payload = msgs[0].getRecords()[0].getPayload();
        String textEncoding = ((payload[0] &amp; 128) == 0) ? &quot;UTF-8&quot; : &quot;UTF-16&quot;; // Get the Text Encoding
        int languageCodeLength = payload[0] &amp; 0063; // Get the Language Code, e.g. &quot;en&quot;
        // String languageCode = new String(payload, 1, languageCodeLength, &quot;US-ASCII&quot;);

        try {
            // Get the Text

            text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
        } catch (UnsupportedEncodingException e) {
            Log.e(&quot;UnsupportedEncoding&quot;, e.toString());
        }


        tvNFCContent.setText(text+&quot;(( taille de la trame: &quot; + text.length () +&quot;))&quot;);
        //lenght.setText(text.length ());
        //tramemail.setText(text+&quot;(( taille de la trame: &quot; + text.length () +&quot;))&quot; + Version);


        Nbre_defauts1.setText(&quot;Le Nombre de d&#233;fauts enregistr&#233;s sur le d&#233;tecteur 1 est de &quot;+ text.charAt(39)+ text.charAt(40));
        Nbre_defauts2.setText(&quot;Le Nombre de d&#233;fauts enregistr&#233;s sur le d&#233;tecteur 2 est de &quot;+ text.charAt(41)+ text.charAt(42));
        Nbre_defauts3.setText(&quot;Le Nombre de d&#233;fauts enregistr&#233;s sur le d&#233;tecteur 3 est de &quot;+ text.charAt(43)+ text.charAt(44));
        Nbre_defauts4.setText(&quot;Le Nombre de d&#233;fauts enregistr&#233;s sur le d&#233;tecteur 4 est de &quot;+ text.charAt(45)+ text.charAt(46));
        Nbre_defauts_pion.setText(&quot;Le Nombre de d&#233;fauts enregistr&#233;s sur l&#39;autoprotection est de &quot;+ text.charAt(57)+ text.charAt(58));
        Nbre_defauts_capot.setText(&quot;Le Nombre de d&#233;fauts enregistr&#233;s sur le capot est de &quot;+ text.charAt(55)+ text.charAt(56));
        Telecommande_trame.setText(&quot;La T&#233;l&#233;commande actuelle a comme num&#233;ro &quot;+ text.charAt(63)+ text.charAt(64)+ text.charAt(65)+ text.charAt(66) );


       if (text.charAt(6) == &#39;O&#39;)
        {
            Etat_centrale_texte.setText( &quot;Centrale en Parfait &#233;tat&quot;);
            Etat_centrale_couleur.setBackgroundResource(R.color.vert);
        } else if (text.charAt(6) == &#39;A&#39;)
       {
           Etat_centrale_texte.setText( &quot;  Centrale en Alarme&quot;);
           Etat_centrale_couleur.setBackgroundResource(R.color.rouge);
       }
       else
       {
           Etat_centrale_texte.setText( &quot;Centrale &#224; revoir&quot;);
           Etat_centrale_couleur.setBackgroundResource(R.color.orange);
       }



        if (text.charAt(7) == &#39;A&#39;) {
            D1.setBackgroundResource(R.drawable.ic_warning_red);
        } else if (text.charAt(7) == &#39;O&#39;) {
            D1.setBackgroundResource(R.drawable.ic_ok);
        } else if (text.charAt(7) == &#39;W&#39;) {
            D1.setBackgroundResource(R.drawable.ic__warning_orange);
        } else {
            D1.setBackgroundResource(R.drawable.ic_baseline_block_24);
        }
        if (text.charAt(8) == &#39;A&#39;) {
            D2.setBackgroundResource(R.drawable.ic_warning_red);
        } else if (text.charAt(8) == &#39;O&#39;) {
            D2.setBackgroundResource(R.drawable.ic_ok);
        } else if (text.charAt(8) == &#39;W&#39;) {
            D2.setBackgroundResource(R.drawable.ic__warning_orange);
        } else {
            D2.setBackgroundResource(R.drawable.ic_baseline_block_24);
        }

        if (text.charAt(9) == &#39;A&#39;) {
            D3.setBackgroundResource(R.drawable.ic_warning_red);
        } else if (text.charAt(9) == &#39;O&#39;) {
            D3.setBackgroundResource(R.drawable.ic_ok);
        } else if (text.charAt(9) == &#39;W&#39;) {
            D3.setBackgroundResource(R.drawable.ic__warning_orange);
        } else {
            D3.setBackgroundResource(R.drawable.ic_baseline_block_24);
        }

        if (text.charAt(10) == &#39;A&#39;) {
            D4.setBackgroundResource(R.drawable.ic_warning_red);
        } else if (text.charAt(10) == &#39;O&#39;) {
            D4.setBackgroundResource(R.drawable.ic_ok);
        } else if (text.charAt(10) == &#39;W&#39;) {
            D4.setBackgroundResource(R.drawable.ic__warning_orange);
        } else {
           D4.setBackgroundResource(R.drawable.ic_baseline_block_24);
        }


        if (text.charAt(59) == &#39;A&#39;) {
            Capot.setBackgroundResource(R.drawable.capot_rouge);
        Capot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), &quot;Le capot est en alarme&quot;, Toast.LENGTH_SHORT).show();
            }
        });}
        else if (text.charAt(59)== &#39;O&#39;){
            Capot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), &quot;Le capot est en Bon &#233;tat&quot;, Toast.LENGTH_SHORT).show();
                }
            });
            Capot.setBackgroundResource(R.drawable.capot_vert);
        }

        else {
            Capot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), &quot;Attention au capot!&quot;, Toast.LENGTH_SHORT).show();
                }
            });
            Capot.setBackgroundResource(R.drawable.capot_orange);
        }

        if (text.charAt(60) == &#39;A&#39;) {
            Autoprotection.setBackgroundResource(R.drawable.tamper_rouge);}
        else if (text.charAt(60)== &#39;O&#39;){
            Autoprotection.setBackgroundResource(R.drawable.pion_vert);
        }

        else {
            Autoprotection.setBackgroundResource(R.drawable.tamper_rouge);
        }


        if (text.charAt(61) == &#39;P&#39;) {
            Mode_alimentation.setText(&quot;L&#39;Alimentation est en mode Secteur&quot;);
            Alimentation.setBackgroundResource(R.drawable.ic_baseline_power_vert);

        }
        else if (text.charAt(61)== &#39;B&#39;) {
            Mode_alimentation.setText(&quot;L&#39;Alimentation est en mode Boost&quot;);
            Alimentation.setBackgroundResource(R.drawable.ic_baseline_power_bleu);
        }
        else {
            Mode_alimentation.setText(&quot;L&#39;Alimentation est absente&quot;);
            Alimentation.setBackgroundResource(R.drawable.ic_baseline_power_rouge);

        }
        if (text.charAt(62) == &#39;H&#39;) {
            Batterie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), &quot;Batterie pleine&quot;, Toast.LENGTH_SHORT).show();
                }
            });
            Batterie.setBackgroundResource(R.drawable.ic_baseline_battery_full_24); }
        else if (text.charAt(62)== &#39;M&#39;){
            Batterie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), &quot;Batterie &#224; moiti&#233; charg&#233;e&quot;, Toast.LENGTH_SHORT).show();
                }
            });
            Batterie.setBackgroundResource(R.drawable.ic_baseline_battery_alert_24);
        }
        else if (text.charAt(62)== &#39;L&#39;){
            Batterie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), &quot;Batterie faible&quot;, Toast.LENGTH_SHORT).show();
                }
            });
            Batterie.setBackgroundResource(R.drawable.ic_baseline_battery_alert_rouge);
        }
        else {
            Batterie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), &quot;Batterie absente&quot;, Toast.LENGTH_SHORT).show();
                }
            });
           Batterie.setBackgroundResource(R.drawable.batt_absente);
        }


        //int version = (int)text.charAt(4);
        if (text.charAt(67)==&#39;R&#39;) {
            Telecommande_centrale.setText(&quot;La T&#233;l&#233;commande arret centrale est Rouge &quot;+ text.charAt(68)+ text.charAt(69)+ text.charAt(70)+ text.charAt(71));
        }

        else if(text.charAt(67)==&#39;B&#39;) {
            Telecommande_centrale.setText(&quot;La T&#233;l&#233;commande arret centrale est Bleu &quot;+ text.charAt(68)+ text.charAt(69)+ text.charAt(70)+ text.charAt(71));
        }
        else{
            Telecommande_centrale.setText(&quot;La T&#233;l&#233;commande arret centrale est Inconnue &quot;+ text.charAt(68)+ text.charAt(69)+ text.charAt(70)+ text.charAt(71));
        }


        if (text.charAt(72)==&#39;R&#39;) {
            Telecommande_arret.setText(&quot;La T&#233;l&#233;commande arret alarme est Rouge &quot;+ text.charAt(73)+ text.charAt(74)+ text.charAt(75)+ text.charAt(76));
        }

        else if(text.charAt(72)==&#39;B&#39;) {
            Telecommande_arret.setText(&quot;La T&#233;l&#233;commande arret alarme est Bleu &quot;+ text.charAt(73) + text.charAt(74) + text.charAt(75) + text.charAt(76));
        }
        else{
            Telecommande_arret.setText(&quot;La T&#233;l&#233;commande arret alarme est Inconnue &quot;+ text.charAt(73) + text.charAt(74) + text.charAt(75) + text.charAt(76));
        }
    }

huangapple
  • 本文由 发表于 2020年7月23日 15:03:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/63048663.html
匿名

发表评论

匿名网友

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

确定