android studio和风天气字体图标校对例程

android studio 版本:2021.2.1

例程名称:hefengiconfonttest

之前在我的项目里用到了和风图标字体,也写了一个如何用这个字体。详见:

android 显示和风天气字体图标

那个代码用了54年判断来显示图标,代码很简单但也很难看,于是有空就做了一个用json的方法改写了一下。也用来校对代码的正确性,写了这个例程。

原来判断图标值和显示图标的代码是这样的:

                if (iconnight.equals("150")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("151")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("152")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("153")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("104")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("350")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("351")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("302")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("303")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("304")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("305")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("306")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("307")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("308")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("309")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("310")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("311")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("312")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("313")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("314")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("315")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("316")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("317")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("318")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("399")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("400")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("401")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("402")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("403")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("404")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("405")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("456")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconnight.equals("457")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("408")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("409")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("410")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("499")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("500")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("501")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("502")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("503")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("504")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("507")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("508")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("509")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("510")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("511")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("512")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("513")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("514")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("515")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("900")) {
                    textView.setText(Html.fromHtml(""));
                } else if (iconday.equals("901")) {
                    textView.setText(Html.fromHtml(""));
                } else {
                    textView.setText(Html.fromHtml(""));
                }

现在是这样的:

public void finish(View view) {
        tianqicode=editText.getText().toString();
        analyzeJSONArray(tubiaoDay);
    }

    String tubiaoDay="{" +
            "'100':'','101':'','102':''," +
            "'103':'','104':'','300':''," +
            "'301':'','302':'','303':''," +
            "'304':'','305':'','306':''," +
            "'307':'','308':'','309':''," +
            "'310':'','311':'','312':''," +
            "'313':'','314':'','315':''," +
            "'316':'','317':'','318':''," +
            "'399':'','400':'','401':''," +
            "'402':'','403':'','404':''," +
            "'405':'','406':'','407':''," +
            "'408':'','409':'','410':''," +
            "'499':'','500':'','501':''," +
            "'502':'','503':'','504':''," +
            "'507':'','508':'','509':''," +
            "'510':'','511':'','512':''," +
            "'513':'','514':'','515':''," +
            "'900':'','901':''}" ;

    public void analyzeJSONArray(String json) {
        try {
            JSONObject jsonObjectALL = new JSONObject(json);
            String b=jsonObjectALL.getString(tianqicode);
            textView.setText(Html.fromHtml(b));
            System.out.println(tianqicode);
            System.out.println(b+"数据");
        } catch (Exception e) {
            //e.printStackTrace();
            textView.setText(Html.fromHtml(""));//如果未找到则显示N/A
        }
    }

看起来清爽很多,好像效率也高了很多,我也不知道。

全部代码:

mainactivity.java

package com.example.hefengiconfonttest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private EditText editText;
    public String tianqicode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tianqifont);
        editText=(EditText)findViewById(R.id.tianqicode);

        Typeface font = Typeface.createFromAsset(getAssets(), "qweather-icons.ttf");//加载图标字体
        textView.setTypeface(font);//设置textView使用图标字体。
    }
    public void finish(View view) {
        tianqicode=editText.getText().toString();
        analyzeJSONArray(tubiaoDay);
    }

    String tubiaoDay="{" +
            "'100':'','101':'','102':''," +
            "'103':'','104':'','300':''," +
            "'301':'','302':'','303':''," +
            "'304':'','305':'','306':''," +
            "'307':'','308':'','309':''," +
            "'310':'','311':'','312':''," +
            "'313':'','314':'','315':''," +
            "'316':'','317':'','318':''," +
            "'399':'','400':'','401':''," +
            "'402':'','403':'','404':''," +
            "'405':'','406':'','407':''," +
            "'408':'','409':'','410':''," +
            "'499':'','500':'','501':''," +
            "'502':'','503':'','504':''," +
            "'507':'','508':'','509':''," +
            "'510':'','511':'','512':''," +
            "'513':'','514':'','515':''," +
            "'900':'','901':''}" ;

    public void analyzeJSONArray(String json) {
        try {
            JSONObject jsonObjectALL = new JSONObject(json);
            String b=jsonObjectALL.getString(tianqicode);
            textView.setText(Html.fromHtml(b));
            System.out.println(tianqicode);
            System.out.println(b+"数据");
        } catch (Exception e) {
            //e.printStackTrace();
            textView.setText(Html.fromHtml(""));
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tianqifont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&#xF102;"
        android:textColor="#E91E63"
        android:textSize="100sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.393" />

    <EditText
        android:id="@+id/tianqicode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="76dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="天气代码"
        android:selectAllOnFocus="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        android:textSize="20sp"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="finish"
        android:text="确定"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.218" />

</androidx.constraintlayout.widget.ConstraintLayout>

加入字体的方法没写,详见:

android 显示和风天气字体图标

动图展示:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kim5659

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值