感觉到自己有必要学习下手机开发方面的知识,不论是为了以后的工作需求还是目前的公司项目。

当然,任何新东西的开始,必然伴随着第一个HelloWorld,Android学习也不例外。既然才开始,我就不做过多的描述了。

对于Android开发的IDE:ADT来说,打开的第一眼有点迷糊,不过看了网上各种目录结构的介绍,慢慢的就明白了,做这个实例,我们尤其需要关注两个地方,一个是src目录,一个就是res目录下的layout目录。src目录放置的是code-behind源码,而layout目录放置的则是xml前台配置文件。

既然我们要实现的功能是点击按钮,然后EditText中显示“Hello World!”。让我们先打开layout文件,拖放一个Button上去,然后拖放一个EditText上去,最后的xml文件结构如下:

<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" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Button" /> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="EditText" /> </RelativeLayout>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

然后在后台代码文件中,我们需要引入两个命名空间:

import android.widget.Button;
import android.widget.EditText;

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

全部代码如下:

package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private Button myButton;
private EditText myText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myButton = (Button)findViewById(R.id.button1);
myText = (EditText)findViewById(R.id.editText1); myButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
myText.setText("Hello World!");
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
 
然后点击运行按钮,在虚拟机界面中点击按钮,得到的结果如下图:
 

这节就到这里了,下面让我们继续探秘吧。


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Android开发探秘之一:创建可以点击的Button的更多相关文章

  1. Android开发之去掉listview的点击效果,一行代码间接粗暴,解决你的问题。

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Android开发之去掉listview的点击效果,一行代码间接粗暴,解决你的问题. 当你在用list ...

  2. Android基础之用Eclipse搭建Android开发环境和创建第一个Android项目(Windows平台)

    一.搭建Android开发环境 准备工作:下载Eclipse.JDK.Android SDK.ADT插件 下载地址:Eclipse:http://www.eclipse.org/downloads/ ...

  3. Android开发-API指南-创建 Content Provider

    Creating a Content Provider 英文原文:http://developer.android.com/guide/topics/providers/content-provide ...

  4. Android开发,Eclipse创建aidl接口时,出错

    Android开发中,当我们需要调用远程Service时,我们一般通过远程接口(RMI)来实现的,而Android的RMI需要AIDL(Android Interface Definition Lan ...

  5. Android开发——Accessibility机制实现模拟点击(微信自动抢红包实现)

    1. 何为Accessibility机制 许多Android使用者因为各种情况导致他们要以不同的方式与手机交互.对于那些由于视力.听力或其它身体原因导致不能方便使用Android智能手机的用户,And ...

  6. Android开发 MMS支持 创建和编辑MMS

    Composing and editing MMS在Android Mms 应用里面的具体实现形式,或数据结构是SlideshowModel,它是一个每个节点为SlideModel的 ArrayLis ...

  7. Android开发探秘之四:利用Intent实现数据传递

    在Android开发过程中,很多人都熟悉Intent,这是个用于在多个View之间共享数据的类.本节主要是继承上节,通过点选ListView中的文本,把文本中的URL加载到一个新的页面上,并且打印出来 ...

  8. Android开发探秘之二:导入存在的项目及其注意事项

    网上看到有jsoup写的例子,就下载下来进行了研究,但是发现不会导入,于是就百度一下,发现了方法:也就是依次点击“File”->“Import”->“General”->“Exist ...

  9. Android开发--Activity的创建

    1.Activity概述 Activity是android四大基本组件之一.每一个activity文件对应一个界面,一个程序由多个activity组成. 2.Android工作目录

随机推荐

  1. IOS开发之实现App消息推送(最新)

    好久没有写过博客啦,今天就由本菜鸟给大家做一个简单的IOSApp消息推送教程吧!一切从0开始,包括XCode6, IOS8, 以及苹果开发者中心最新如何注册应用,申请证书以及下载配置概要文件,相信很多 ...

  2. Effective Java 68 Prefer executors and tasks to threads

    Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...

  3. Struts2 Spring Hibernate等各个版本下载推荐

    推荐jar包下载地址: http://mvnrepository.com/ 应有尽有

  4. 【PHP】$_POST, $HTTP_RAW_POST_DATA, and php://input

    1.HTML <form> enctype Attribute application/x-www-form-urlencoded  传送之前所有的字符都会被encoded,(spaces ...

  5. 【nginx】配置文件的优化

    1.编译安装过程优化 在编译Nginx时,默认以debug模式进行,而在debug模式下会插入很多跟踪和ASSERT之类的信息,编译完成后,一个Nginx要有好几兆字节.在编译前取消Nginx的deb ...

  6. MySQL的replace函数的用法

    REPLACE(field,find_str,replace_str): 字段field的内容中的find_str 将被 替换为 replace_str . 例如: update short_url ...

  7. 续Gulp使用入门-综合运用>使用Gulp构建一个项目

    这是我的文件目录结构图  下面是我gulpfile.js的配置 'use strict' var gulp=require('gulp'); var gutil=require('gulp-util' ...

  8. 10分钟制作UWP汉堡菜单

    什么是汉堡菜单? 汉堡菜单,指的是一个可以弹出和收回的侧边栏.在UWP和Android应用中,汉堡菜单都非常常见. 首先我们列出所有需要掌握的前置知识: 1,SplitView 2,StackPane ...

  9. 迅为iTOP-4412开发板Ubuntu操作系统烧写方法

    本文转自:http://www.topeetboard.com Ubuntu系统烧写硬件平台:iTOP4412开发板 需要准备:串口线.读卡器.一张SD卡最好是2G(自备).另外一张卡(大于等于2G都 ...

  10. CentOS 6.5部署安装Memcached

    1. Yum安装Memcache 查找memcached yum  search  memcached 该命令可以查询yum库中有关memcached的安装包信息,以下是搜寻结果截图: 安装 memc ...