Android 实现简单天气应用

引导页面,多个城市的天气,可以通过滑动来翻阅。

先看下截图:

1、城市天气界面

Android 实现简单天气应用   

2、引导界面

Android 实现简单天气应用

应用引导页面

package org.qxj.iweather.page;

import org.qxj.iweather.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

public class Welcome extends Activity {

    private static final String TAG = "Welcome";
    private SharedPreferences shared = null;
    private SharedPreferences.Editor edit = null;
    private LinearLayout layout = null;
    private ViewFlipper flip = null;
    private LayoutParams match = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    private String isFirst = "false";
    private Intent intent = null;

    class TurnPage implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(5000);
                TurnToMain();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.shared = super.getSharedPreferences("IWeather", MODE_PRIVATE);
        this.edit = this.shared.edit();

        this.layout = new LinearLayout(this);
        this.layout.setOrientation(LinearLayout.VERTICAL);

        this.flip = new ViewFlipper(this);
        this.layout.addView(this.flip, match);
        this.layout.setBackgroundResource(R.drawable.welcome);
        super.addContentView(this.layout, match);

        isFirst = this.shared.getString("isFirst", "true");
        Log.i(TAG, "isFirst: " + isFirst);
        if ("true".equals(isFirst)) {
            this.edit.putString("isFirst", "false");
            this.edit.commit();
            new Thread(new TurnPage()).start();
        } else {
            // 进行跳转
            TurnToMain();
        }

    }

    public void TurnToMain() {
        // 进行跳转
        intent = new Intent(Welcome.this, Main.class);
        startActivity(intent);
        // 销毁该Activity,返回的时候,不会返回该界面。
        this.finish();
    }

}

主页面

package org.qxj.iweather.page;

import org.qxj.iweather.R;
import org.qxj.iweather.Contents.IWeather;
import org.qxj.iweather.net.HttpHelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

public class Main extends Activity implements OnGestureListener {

    private IWeather iWeather = null;
    private LinearLayout layout = null;
    private GestureDetector detector;
    private LayoutParams match = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.layout = new LinearLayout(this);
        detector = new GestureDetector(this);
        this.iWeather = new IWeather(this);
        this.layout.addView(this.iWeather.flip, match);
        this.addContentView(this.layout, match);
        iWeather.flip.addView(new PageLayout(this, "101301301"));
        iWeather.flip.addView(new PageLayout(this, "101010100"));
        iWeather.flip.addView(new PageLayout(this, "101260101"));
        iWeather.flip.addView(new PageLayout(this, "101190501"));
        iWeather.flip.addView(new PageLayout(this, "101190101"));
        iWeather.flip.addView(new PageLayout(this, "101170101"));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.detector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        if (e1.getX() - e2.getX() > 80) {
            iWeather.flip.setInAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.push_left_in));
            iWeather.flip.setOutAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.push_left_out));
            iWeather.flip.showNext();
            return true;
        } else if (e1.getX() - e2.getX() < -80) {
            iWeather.flip.setInAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.push_right_in));
            iWeather.flip.setOutAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.push_right_out));
            iWeather.flip.showPrevious();
            return true;
        }
        return false;
    }

    @Override
    public void onLongPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {
        // TODO Auto-generated method stub
        return false;
    }

}

自定义的pageLayout

package org.qxj.iweather.page;

import org.qxj.iweather.R;
import org.qxj.iweather.model.Weather;
import org.qxj.iweather.net.HttpHelper;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PageLayout extends LinearLayout {

    private Weather weather = null;
    private Context context = null;
    private TextView temperature = null;
    private TextView city = null;
    private TextView statues = null;
    private TextView date = null;
    public Handler hander = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
            case 1:
                weather = (Weather) msg.obj;
                init();
                break;
            case 0:
                Toast.makeText(context, "数据操作失败!", Toast.LENGTH_SHORT).show();
                break;
            }
        }

    };

    public PageLayout(Context context, String city) {
        super(context);
        this.context = context;
        new Thread(new HttpHelper(city, this)).start();
    }

    public PageLayout(Context context, AttributeSet attrs, String city) {
        super(context, attrs);
        this.context = context;
        new Thread(new HttpHelper(city, this)).start();
    }

    public PageLayout(Context context, AttributeSet attrs, int defStyle, String city) {
        super(context, attrs, defStyle);
        this.context = context;
        new Thread(new HttpHelper(city, this)).start();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.page_layout, this);
        this.temperature = (TextView) findViewById(R.id.tempearture);
        this.city = (TextView) findViewById(R.id.city);
        this.statues = (TextView) findViewById(R.id.statues);
        this.date = (TextView) findViewById(R.id.date);

        this.temperature.setText(this.weather.getTemp1());
        this.city.setText(this.weather.getCity());
        this.statues.setText(this.weather.getWeather1());
        this.date.setText(this.weather.getDate_y());
        this.setBackgroundByWeather(this.weather.getWeather1());
    }

    /**
     * 设置屏幕背景
     */
    private void setBackgroundByWeather(String w) {
        if (w.indexOf("雪") != -1) {
            // 有雪的天气
            this.setBackgroundResource(R.drawable.snow);
        } else if (w.indexOf("雨") != -1) {
            // 下雨天
            this.setBackgroundResource(R.drawable.rain);
        } else if (w.indexOf("晴") != -1) {
            //
            this.setBackgroundResource(R.drawable.sun);
        } else if (w.indexOf("阴") != -1) {
            // 阴天
            this.setBackgroundResource(R.drawable.water);
        } else {
            this.setBackgroundResource(R.drawable.sun);
        }
    }

}

完整代码:

http://download.csdn.net/detail/niitqin/6472535