android_我的第一个Android程序
2024-08-23 01:54:58
今天开始学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();
}
最新文章
- jquery控制文字内容溢出用点点点(…)省略号表示
- js高级程序设计(七)BOM
- vb.net下载代码
- The prefix ";mvc"; for element ";mvc:annotation-driven"; is not bound 的解决方法
- 如何调节datagridview中字体
- html5之canvas练习
- 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)
- 我也要这样写define、、
- 判断http 请求来自于手机还是PC
- CentOS克隆机器步骤,图文教程
- Poj3321 Apple tree
- SoupUI安装
- Android短信验证码倒计时
- Azkaban安装及分布式部署(multiple-executor)
- PHP array
- Ubuntu cd
- rpm-yum_install_software
- redis缓存数据架构实战
- MyBatis基本配置和实践(二)
- c++我在努力----第三次作业体会
热门文章
- 【hdu4436/LA6387-str2int】sam处理不同子串
- 【反演复习计划】【bzoj1011】zap-queries
- js面向对象编程(二)构造函数的继承(转载)
- Selenium2+python自动化73-定位的坑:class属性有空格【转载】
- Juel Getting Started
- 正则表达式、re、常用模块
- 蓝牙遥控小车设计(三)——Amarino和 Android手机重力感应控制
- EASYUI datagrid批量修改与提交
- (转帖)关于easyui中的datagrid在加载数据时候报错:无法获取属性";Length";的值,对象为null或未定义
- 51nod 1344 走格子【贪心/前缀和】