主要参考《第一行代码》

1.TextView:

功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息

如:

 <TextView

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textColor="#0000ff"

         android:textSize="40sp"

         android:text="@string/hello_world" />

显示效果:

上面的xml代码中,设置了几个常用的属性:

android:layout_width和android:layout_height分别设置控件的宽高

textColor设置显示的文本的颜色

textSize设置显示的文本的字体大小

text设置显示的文本内容。

2.Button:

前面用到的比较多,经常被用到的就是通过id获取按钮,然后绑定单击监听事件,这里仅列举个例子:

activity_main.xml:

 <TextView

         android:id="@+id/tv"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textColor="#0000ff"

         android:textSize="40sp"

         android:text="@string/hello_world" />

     <Button

         android:id="@+id/btn"

         android:layout_below="@id/tv"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="@string/btnText"/>

MainActivity.java:

 public class MainActivity extends ActionBarActivity {

     private TextView tv;

       private Button btn;

       @Override

     protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

         btn = (Button) findViewById(R.id.btn);

         tv = (TextView) findViewById(R.id.tv);

         btn.setOnClickListener(new OnClickListener() {

                  @Override

                  public void onClick(View v) {

                       // TODO Auto-generated method stub

                       tv.setText("It's changed!");

                  }

            });

     }

 }

3.EditText:

即文本输入框,如下修改程序,在按钮之上添加一个EditText,点击按钮,会获取EditText的值并把它设置为TextView的Text属性:

activity_main.xml:

  <TextView

         android:id="@+id/tv"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textColor="#0000ff"

         android:textSize="40sp"

         android:text="@string/hello_world" />

     <EditText

         android:id="@+id/et"

         android:layout_below="@id/tv"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:hint="@string/hintText"

         />

       <Button

         android:id="@+id/btn"

         android:layout_below="@id/et"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="@string/btnText"/>

MainActivity.java:

 public class MainActivity extends ActionBarActivity {

     private TextView tv;

       private Button btn;

       private EditText et;

       @Override

     protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

         btn = (Button) findViewById(R.id.btn);

         tv = (TextView) findViewById(R.id.tv);

         et = (EditText)findViewById(R.id.et);

         btn.setOnClickListener(new OnClickListener() {

                  @Override

                  public void onClick(View v) {

                       // TODO Auto-generated method stub

                       Editable text = et.getText();

                       tv.setText(text.toString());

                  }

            });

     }

 }

运行效果:

输入值,然后点击按钮:

注意到由于EditText的layout_height属性是wrap_content,所以会随着输入内容的增多不断变大,影响整体布局。若想固定其高度,可以设置maxLines属性,设置最多只显示的行数,其他内容向上滚动

如:android:maxLines = “1”

EditText的高度就不会变化了。

4.ImageView:

使用来显示图片的一个控件,之前的程序中曾经用到过,当然,它最主要的属性肯定是要显示图片的来源了,即android:src属性,将要显示的图片存放在res/drawable中,如图片名为hero.png。要显示该图片,设置android:src=”@drawable/hero”即可。

 <ImageView

         android:id="@+id/iv"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:src="@drawable/hero"/>

显示结果:

5.ProgressBar:

即进度条,使用style属性,可以设置不同的显示风格:

1)不设置style属性或者设置为style="?android:attr/progressBarStyle" ,环形显示

2)style="?android:attr/progressBarStyleHorizontal",水平横条显示

3)style="?android:attr/progressBarStyleLarge",大号的环形显示

4)style="?android:attr/progressBarStyleSmall",小号的

进度条当然是用来显示进度的,通过findViewById()获取ProgressBar,然后使用setProgress()就可以设置当前进度,使用getProgress()可以获取当前进度。

如:

布局代码:

 <ProgressBar

           android:id="@+id/pb"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           style="?android:attr/progressBarStyleHorizontal"

           android:max="100"

           />

       <Button

           android:id="@+id/btn"

           android:layout_below="@id/pb"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/add_progress"/>

Activity代码:

 public class MainActivity extends ActionBarActivity {

     private ProgressBar pb;

       private Button btn;

       @Override

     protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_main);

         pb = (ProgressBar) findViewById(R.id.pb);

         btn = (Button) findViewById(R.id.btn);

         Log.i("PB",pb.getProgress()+"");

         btn.setOnClickListener(new OnClickListener() {

                  @Override

                  public void onClick(View v) {

                       // TODO Auto-generated method stub

                       Log.i("PB",pb.getProgress()+"");

                       pb.setProgress(pb.getProgress()+10);

                  }

            });

     }

 }

运行结果:

初始时,默认进度为0

多次点击按钮之后:

达到android:max所设置的最大值后,再加也不会有变化了。

6.AlertDialog:

这个控件就是弹出一个对话框,类似于桌面开发中的模态对话框,必须关闭该对话框,才能进行后续交互操作,可用于显示比较重要的内容。

AlertDialog的构造方法都是protected,没法直接通过构造来创建AlertDialog,但是可以通过其内部类Builder来创建。

具体使用可以参考帮助手册中关于这个内部类的帮助信息,下面举个简单例子:

 AlertDialog.Builder dialog = new AlertDialog.Builder(this);

         dialog.setTitle("Warning");

         dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                  @Override

                  public void onClick(DialogInterface dialog, int which) {

                       // TODO Auto-generated method stub

                  }

            });

         dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

                  @Override

                  public void onClick(DialogInterface dialog, int which) {

                       // TODO Auto-generated method stub

                  }

            });

         dialog.setMessage("warning, hahaha");

         dialog.show();

运行结果:

7.ProgressDialog:

类似于AlertDialog,也是对话框,不过它显示的内容是一个进度条,好像是对话框和进度条两个控件的结合。

 ProgressDialog pd = new ProgressDialog(this);

 pd.setTitle("Data Loading...");

 pd.show();

运行结果:

android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件的更多相关文章

  1. android菜鸟学习笔记14----Android控件(三) ListView的简单使用

    MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...

  2. android菜鸟学习笔记30----Android使用百度地图API(一)准备工作及在应用中显示地图

    1.准备工作: 百度地图API是免费开放的,但是需要申请API Key: 1)先注册一个百度开发者帐号 2)进入百度开放服务平台http://developer.baidu.com/ 3)进入LBS云 ...

  3. android菜鸟学习笔记7----android布局(二)

    3.FrameLayout:帧布局 如同Flash或者photoshop中图层的概念,在上面的图层遮盖下面的图层,没被遮到的地方仍然显示出来. 右击res/layout,然后在弹出的菜单中选择new, ...

  4. android菜鸟学习笔记31----Android使用百度地图API(二)获取地理位置及地图控制器的简单使用

    1.获取当前地理位置: Android中提供了一个LocationManager的类,用于管理地理位置.不能通过构造函数获取该类的实例,而是通过Context的getSystemService(): ...

  5. android菜鸟学习笔记21----ContentProvider(一)ContentProvider的简单使用

    ContentProvider是Android四大组件之一,它用来封装数据,并通过ContentResolver接口将数据提供给其他应用.只有当需要在多个应用之间共享数据时才会用到ContentPro ...

  6. android菜鸟学习笔记8----Activity(一)

    Activity是android应用程序中重要的组件之一,常听到的android四大组件是Activity.Service.BroadcastReceiver和ContentProvider.它间接继 ...

  7. android菜鸟学习笔记29----Android应用向用户发送提示信息的方式总结

    常见的向用户发送提示信息的方式有3种,分别为: 1)发送Toast信息 2)弹出对话框 3)发送通知 总结如下: 方式1:发送Toast信息: 这种方式最简单,在之前的学习中多次使用过.Toast是在 ...

  8. android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据

    主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: U ...

  9. android菜鸟学习笔记2----关于adb

    adb : android debug bridge android调试桥 路径:adt-bundle目录/sdk/platform-tools/adb.exe 常见的adb命令: adb devic ...

随机推荐

  1. YACEP相关技术工具服务技巧(上)

    这篇随笔的核心是介绍一下YACEP所用到的一些技术,工具,服务和技巧,鉴于篇幅原因,不可能面面俱到,只能点到为止,目录如下: 目录: 1. YACEP简介(上)             2. 技术篇( ...

  2. MySQL复制表结构和内容到另一张表(转)

    MySQL不要看它小,一个开源的产物,要学习它的东西真的很多.而它的一切是SQL Server无法比拟的. 复制表结构及数据到新表 create table 新表 select * from 旧表 只 ...

  3. If Value Exists Then Query Else Allow Create New in Oracle Forms An Example

    An example given below for Oracle Forms, when a value exists then execute query for that value to di ...

  4. openssl之EVP系列之12---EVP_Seal系列函数介绍

    openssl之EVP系列之12---EVP_Seal系列函数介绍     ---依据openssl doc/crypto/EVP_SealInit.pod翻译和自己的理解写成     (作者:Dra ...

  5. Protel中的快捷键使用(网上资源)

    使用快捷键之前,将输入法切换至中文(中国)状态 Enter——选取或启动 Esc——放弃或取消 F1——启动在线帮助窗 Tab——启动浮动图件的属性窗口 Page Up——放大窗口显示比例 Page ...

  6. 2017.2.21 activiti实战--第七章--Activiti与spring集成(一)配置文件

    学习资料:<Activiti实战> 第七章 Activiti与容器集成 本章讲解activiti-spring可以做的事情,如何与现有系统集成,包含bean的注入.统一事务管理等. 7.1 ...

  7. struts2注解总结----@Action和@Result

    除了使用配置文件配置之外,还能够使用注解来配置 以下是一些经常使用的注解 介绍: @Action/@Actions: @Action指定一个类为action,相应配置文件里的<action> ...

  8. 微信小程序 - 对象转换成对象数组

    后端传过来的一个个对象 {1,2,3,4},{1,3,5,},{1,3,5} 我们应该转化为数组对象 [{},{},{},{}]  ,最后通过wx:for遍历到页面 示例图:

  9. apache支持php

    #tarzxvf php-5.2.9.tar.gz #cdphp-5.2.9 #./configure--prefix=/usr/local/php --with-apxs2=/usr/local/a ...

  10. #測试相关#Getting “junit.framework.AssertionFailedError: Forked Java VM exited abnormally” Exception

    编写Ant脚本进行持续測试的时候.出现了junit.framework.AssertionFailedError: Forked Java VM exited abnormally的报错,以此为key ...