android_我的第一个Android程序

   今天开始学Android开发,搞了一下午就完成了两个小功能,大部分时间都在调试、熟悉环境,

Android开发环境对比VS无论是安装、使用、更新都不够方便,不过慢慢适应就好
 
完成功能如下:
功能一:显示当前系统时间
功能二:根据编号获取城市天气
 
 
View层:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/show"
        android:onClick="getCurrentDate"
        android:text="获取当前时间:" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="33dp"
        android:onClick="getWuHanWeather"
        android:text="获取城市天气:" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:text="1"
        android:ems="10" >
        
        <requestFocus />
    </EditText>
 
</RelativeLayout>  
 
功能一:显示当前系统时间
    public void getCurrentDate(View source) {
        TextView tv = (TextView) findViewById(R.id.show);
        tv.setText("当前时间:" + new java.util.Date());
    } 
 
功能二:根据编号获取城市天气
    TextView response;
    HttpClient httClient;
 
    public void getWuHanWeather(View source) {
        this.httClient = new DefaultHttpClient();
        this.response = (TextView) findViewById(R.id.show);
 
        accessSecret(source);
    }
 
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                response.setText("");
                response.setText(msg.obj.toString() + "\n");
            }
        }
    };
 
    public void accessSecret(View v) {
        String type = "";
        int inputValue=Integer.parseInt(((EditText) findViewById(R.id.editText1)).getText().toString());
        if (inputValue== 1) {
            type = "101200101";
        } else {
            type = "101200401";
        }
 
        final String cityId = type;
        new Thread() {
            @Override
            public void run() {
                // 创建一个HttpGet对象
                String url = "http://www.weather.com.cn/adat/sk/" + cityId
                        + ".html";
                HttpGet get = new HttpGet(url);
 
                // HttpGet get = new HttpGet(
                // "http://www.weather.com.cn/adat/sk/101200101.html");
                try {
                    // 发送GET请求
                    HttpResponse httpResponse = httClient.execute(get);
                    String json = EntityUtils.toString(
                            httpResponse.getEntity(), "UTF-8");
 
                    Message msg = new Message();
                    msg.what = 0x123;
                    msg.obj = json;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }  
 





posted @ 2015-05-24 17:49    阅读(258)  评论(0编辑  收藏  举报