博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android_我的第一个Android程序
阅读量:6875 次
发布时间:2019-06-26

本文共 2772 字,大约阅读时间需要 9 分钟。

   今天开始学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();
    }  
 

转载地址:http://wtofl.baihongyu.com/

你可能感兴趣的文章
如何解决vim乱码【转载】
查看>>
你如何理解HTML结构的语义化?
查看>>
JQuery Ajax 的简单使用
查看>>
Codeforces Round #287 (Div. 2) ABCDE
查看>>
【转载】读懂IL代码就这么简单(二)
查看>>
09-JS的事件流的概念(重点)
查看>>
有关inline-block
查看>>
文献随笔(九)
查看>>
git相关
查看>>
加入大型的js文件如jQuery文件,Eclipse会报错
查看>>
POJ 2763 (树链剖分+边修改+边查询)
查看>>
全局变量---只创建一次
查看>>
IOS APP上下黑边问题
查看>>
数位dp题集
查看>>
C# 汉字转拼音
查看>>
jquery实现复制的两种方式
查看>>
Django分页(一)
查看>>
Balance Adjustment页面调整无法保存的问题
查看>>
De Moivre–Laplace theorem
查看>>
symfony2使用form指定的checkbox,设置其属性disabled
查看>>