• TextView 文本框
  • EditText控件
  • Button 与 ImageButton
  • ImageView
  • RadioButton
  • CheckBox复选框

TextView 文本框

,用于显示文本的控件
    1) 代码
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MyActivity"
    />

2)TextView的常用属性:

-id: 为TextView设置一个组件id,根据id,我们可以在Java代码中获取到该对象,然后进行相关属性的设置
        -layout_width: 组件宽度,一般有3种属性 wrap_content ,match_parent 和自己输入宽度
            wrap_content表示根据组件大小确定宽度即组件包含文字所占宽度越多,组件越宽;
            match_parent表示填满该组件的父容器即包含了该组件的组件如代码中的LinearLayout ;
        -layout_hight: 组件高度,一般也有3种属性,同上;
        -gravity: 设置控件中内容的对齐方向,即文字内容的对齐方向;
        -text: 设置显示的文本内容(建议把字符串写到values/string.xml文件中,然后使用@String/xxx的方式获取 , 也可以直接写在""中);
        -textColor: 设置文本的颜色(后面会附上常用颜色码);
        -textStyle: 设置文本显示风格,三个可选属性 normal(无效果) ,bold(加粗) ,italic(斜体);
        -background: 控件背景颜色,即填充整个控件的颜色;

3)在textview控件中引入string.xml中写好的文本的方法:

第一步:现在string.xml文件中加入你要显示的文本,并命名:

<?xml version="1.0" encoding="utf-8"?>
            <resources>
                <string name="app_name">MyActivity</string>
                <string name="hello">hello ,你好</string>
            </resources>

第二步:然后在控件中引入:

<TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/hello"
                />

EditText控件

,与TextView控件类似,最大的区别就是EdiText允许用户输入

1) 代码
            <EditText
            android:id="@+id/eidt"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            />
     2)EditText常用属性:
            -id: 为EditText设置一个组件id;
            -layout_weith: 设置控件的宽度;
            -layout_hight: 设置控件的高度;

-inputType(textPassword): 设置输入内容为密码;
            -inputType(Phone): 设置输入的内容为号码;
            -maxLength: 设置输入的最大文本数, 参数为数字例如 android:maxLength=“5”;
            -maxLines: 设置最大行数,参数为数字;
            -minLines: 设置最小行数,参数为数字;
            -hint: 设置默认提示文本;
            -textColorHint: 设置默认提示文本颜色;
            -capitalize: 设置英文字母大小写属性,参数包含:
            sentences: 仅第一个字母大写
            words: 单词首字母大写
            characyers: 全部字母大写;
    3) EditText控件的绑定:
        绑定控件的方法也都一样,在onCreate方法中使用findViewById()方法:

public class MyActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //绑定main.xml文件中的EdiText控件
            EditText editText = (EditText) findViewById(R.id.edit);
            }
        }

4) EditText获取用户输入:

绑定后获取用户的输入很容易,使用getText()方法即可:

editText.getText();//获取用户输入的方法

值得一提的是这个方法直接写在绑定好的EditText语句后面是不行的!准确的说是获取不到输入的内容,我们需要一个 触发事件在输入完成后再触发获取输入的方法,就能正常拿到数据啦!下面看代码:

EditText editText;
            TextView text;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
         
                editText = (EditText) findViewById(R.id.edit);
                text = (TextView) findViewById(R.id.text);
         
                text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        text.setText(editText.getText());;
                    }
                });
         
            }

text.setOnClickListener()方法是text监听方法.用来监听TextView的点击事件。
        整个流程就是在输入文本后点击text然后text的文本就会被替换成你输入的文本.

Button 与 ImageButton

本节学习Android基本控件按钮控件,Button和ImageButton用法基本类似,所以本节重点讲解Button控件。

1)代码:

<Button
            android:id="@+id/btn"
            android:text="普通按钮"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

2)Button的其他常用属性:

-background 背景 可以是颜色或者图片或这xml资源
    -text        按钮显示的文字
    -textColor    按钮文字颜色
    -textSize    按钮文字大小

3)Button控件的绑定和监听:

绑定

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

监听的方法很常用

btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this , "button 被按下" , Toast.LENGTH_LONG).show();
                }
            });

4)Toast方法是常见的黑色提示框,自动消失。

完整代码:

public class MainActivity extends Activity {
         
            private Button btn;//声明全局变量
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
         
                //绑定控件
                btn = (Button) findViewById(R.id.btn);
                //监听控件
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this , "button 被按下" , Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }

ImageView

,ImageView 图像视图,顾名思义,是一个用来显示图片的控件。

1)代码:

<ImageView
            android:id="@+id/imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher"/>

2)ImageView的常用属性:

-background 设置控件背景    当固定宽度时会拉伸图片
    -src        设置控件填充内容    固定宽度时不会拉伸图片

3)绑定

img = (ImageView) findViewById(R.id.imageview);

ImageView没有啥监听方法,一般ImageView用来实现app的图片轮播功能。这会用到修改图片显示的方法

常用的修改方法:

-setImageResource(int id);    使用内部资源图片替换默认图片,id为R.drawable.图片名称
        -setImageBitmap(Bitmap bitmap);    使用bitmap 替换默认图片,bitmap一般通过网络获取

使用方法:

img.setImageResource(R.mipmap.ic_launcher);

RadioButton

,RadioButton单选按钮,就是几个选项只能选中一个。 因此我们要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!

1)代码:

<RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
     
            <RadioButton
                android:id="@+id/btnMan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:checked="true"/>
     
            <RadioButton
                android:id="@+id/btnWoman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>

2)控件的监听方法:

ReadioGroup监听方法:setOnCheckedChangeListener()

3)获取选择的值的方法:getText()

RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
            //第一种获得单选按钮值的方法  
            //为radioGroup设置一个监听器:setOnCheckedChanged()  
            radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton radbtn = (RadioButton) findViewById(checkedId);
                    Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
                }
            });

获得RadioButton相关信息的方法:

-getClidCont() 获得按钮组中的单选按钮的数目
        -getClindAt()  根据索引值获得单选按钮 (参数为索引值 0,1,2,3..)
        -isChecked()   判断按钮是否被选中(参数为true/false)

CheckBox复选框

1)代码:

<CheckBox
            android:id="@+id/checkbox"
            android:text="苹果"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/checkbox1"
            android:text="香蕉"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <CheckBox
            android:id="@+id/checkbox2"
            android:text="大鸭梨"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

2)监听方法:

可以同时选中多个选项,至于获得选中的值,一般是为每个CheckBox添加事件:setOnCheckedChangeListener

public class MyActivity extends Activity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{
         
            private CheckBox cb_one;
            private CheckBox cb_two;
            private CheckBox cb_three;
            private Button btn_send;
         
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
         
                cb_one = (CheckBox) findViewById(R.id.cb_one);
                cb_two = (CheckBox) findViewById(R.id.cb_two);
                cb_three = (CheckBox) findViewById(R.id.cb_three);
                btn_send = (Button) findViewById(R.id.btn_send);
         
                cb_one.setOnCheckedChangeListener(this);
                cb_two.setOnCheckedChangeListener(this);
                cb_three.setOnCheckedChangeListener(this);
                btn_send.setOnClickListener(this);
         
            }
         
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
               if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();
            }
         
             //统一监听
            @Override
            public void onClick(View view) {
                String choose = "";
                if(cb_one.isChecked())choose += cb_one.getText().toString() + "";
                if(cb_two.isChecked())choose += cb_two.getText().toString() + "";
                if(cb_three.isChecked())choose += cb_three.getText().toString() + "";
                Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();
            }
        }

[APP] Android 开发笔记 004-Android常用基本控件使用说明的更多相关文章

  1. Android学习笔记(七)——常见控件

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...

  2. Android开发笔记:Android开发环境搭建

    基于Eclipse开发 1. 安装JDK 首先进入JDK下载页面,选择需要的版本下载安装. JDK 下载地址:https://www.oracle.com/technetwork/java/javas ...

  3. Android学习笔记50:使用WebView控件浏览网页

    在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...

  4. Android开发(21)--有关Spinner控件的使用说明

    下拉列表 Spinner,Spinner是一个每次只能选择所有项的一个项的控件.它的项来自于与之相关联的适配器中. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下 ...

  5. Android开发(二)——自定义圆形控件的使用CircleImageView

    CircleImageView,a fast circular ImageView perfect for profile images. 主要的类CircleImageView: package d ...

  6. [Android学习笔记]继承自ViewGroup的控件的过程学习

    ViewGroup文档 http://developer.android.com/training/index.html 继承自ViewGroup需要重写onLayout方法用来为子View设定位置信 ...

  7. 【Android开发笔记】Android Splash Screen 启动界面

    public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanc ...

  8. Android开发之使用Handler刷新UI控件

    一.为什么必须使用Handler 线程安全问题 这个问题要理解的话很容易,如果没有这个约束,那么同时有两个线程对一个UI控件进行调整,那么控件自然就没法正常的工作,而为了解决这种二义性(就是一个东西同 ...

  9. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

随机推荐

  1. thinkphp 3.2 多表查询 group

    分析一 $res = M('member') ->table('__MEMBER__ as a') ->join('__ORDER__ as b') ->field('a.id,b. ...

  2. 非抢占式RCU中的一些概念

    该记录着重介绍下:2.6.34版本中非抢占式RCU的基本概念. RCU保护的是指针,因为指针的赋值可以使用原子操作完成: 在非抢占式RCU中: 对于读者,RCU仅需要抢占失效,因此获得读锁和释放读锁分 ...

  3. ios模拟器已安装但xcode无法显示

    最近把系统抹盘重装了, 然后用Time Machine恢复到原始状态, 一切安好, 但是使用xcode的时候发现一个模拟器都没有了: 各种折腾, 重装SDK啊, 重装xcode啊,最后发现, 如果你的 ...

  4. 常用Dos(转)

    先介绍一下通配符的概念. 通配符*和? *表示一个字符串 ?只代表一个字符 注意通配符只能通配文件名或扩展名,不能全都表示.例如我们要查找以字母y开头的所有文件,可以输入以下命令:dir y*.*:如 ...

  5. springMVC中如何访问WebContent中的资源文件

    一.问题: 我的工程目录如下: WebContent |-css |-js |-imgs |-META-INF |-WEB-INF |-jsp |-login.jsp 如何在login.jsp中引用i ...

  6. ubuntu alsa

    今天要在linux下搞音频编程,在网上查阅了一下资料,网上很多资料都是在linux下直接对/dev/dsp进行编程的,因为在以往的linux系统中,我们是可以通过cat  xxx.wav /dev/d ...

  7. [Arch] 04. Software Architectural Patterns

    让我们一起 回忆: 原则 基本认识 S 应该仅有一个引起它变化的原因 O 在不被修改的前提下被扩展 L 尽量从抽象类继承 I 应该依赖于抽象 D 倾向瘦接口 让我们开始 新课: [Design Pat ...

  8. C# IsBackground作用

    https://blog.csdn.net/snakorse/article/details/43888847 要点: 1.当在主线程中创建了一个线程,那么该线程的IsBackground默认是设置为 ...

  9. MySQL--指定浮点型数据的精确度TRUNCATE

    INSERT INTO perf_week(node_id,perf_time,pm25,pm10,temp,humi) ) ) ) ) AS humi FROM perf_pm25 WEEK) AN ...

  10. WinInet API详解

    一.概述 WinInet(「Windows Internet」)API帮助程序员使用三个常见的Internet协议,这三个协议是:用于World Wide Web万维网的超文本传输协议(HTTP:Hy ...