Android基本控件简介

来源:互联网 发布:怎么mac的软件卸载不了 编辑:程序博客网 时间:2024/06/15 08:12

Android中的基本控件

在使用Android开发App的过程中,我们会对APP的每一个页面进行排版,而排版的前提就是Android中一些为我们定义并且设置了固定样式的基本控件和高级控件。

<TextView>:故名知意,文本视图,用来显示文字。

<TextView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="我"// 设置显示的文字
            android:textColor="#fff"//文字的颜色
            android:textSize="20sp" //文字的大小
            android:drawableLeft="@mipmap/fanhui"设置文字周围的图片
android:autoLink="phone" //自动连接 可选的值有phone/web/map 等
            android:gravity="center"/>  //设置文字的显示位置

<EditText>:文本编辑框 有点类似前端中的input标签

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
//设置编辑框中可以输入的文本类型

hint   //设置文本编辑框默认显示的文本  

可选的值有number,numberPassword等 

android:maxLength="5":控制输入框中文本的最大长度
        />

<ImageView> 图片视图 类似前端的《image》标签

 <ImageView
        android:layout_width="500dp"
        android:layout_height="500dp"
        android:src="@mipmap/jiaju"
        android:scaleType="centerCrop"
        />

src  图片路径

scaleType     缩放类型

<!--scaleType:默认fitCenter
       
centerCrop:居中裁剪
       
center:按原比例居中显示
    -->

<Button>:按钮

    <Button    android:onClick="showInfo"// 设置按钮的点击事件    android:id="@+id/personInfo"    android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="个人信息"/>
<RadioButton>:单选按钮初始化语句为:
<RadioGroup
       
android:layout_width="match_parent"
      android:id="@+id/myGroup"
       android:layout_height="wrap_content">
       <RadioButton
           android:id="@+id/rb1"
           android:text="男"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
       <RadioButton
           android:id="@+id/rb2"
           android:text="女"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
       <RadioButton
           android:id="@+id/rb3"
           android:text="其他"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />
   </RadioGroup>
注意外面一定得用RadioGroup包裹,否则就起不到单选的效果了。
<CheckBox>复选框
<LinearLayout
            android:id="@+id/check_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="看书"/>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打球"/>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="书法"/>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="收藏"/>
        </LinearLayout>
获取每个checkBox并判断他们是否选中的时候我们可以这样:

public class MainActivity extends AppCompatActivity {

    private LinearLayout check_layout; //外面的父容器

    private Button btn_submit; //绑定的点击按钮

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        check_layout= (LinearLayout) findViewById(R.id.check_layout);

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

        btn_submit.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                int childCount=check_layout.getChildCount();

                StringBuffer buffer=new StringBuffer();

                for (int i=0;i<childCount;i++){

                    CheckBox checkBox= (CheckBox) check_layout.getChildAt(i);

                    if (checkBox.isChecked()){

                        buffer.append(checkBox.getText()+"\n");

                    }

                }

                Toast.makeText(MainActivity.this, buffer.toString(), Toast.LENGTH_SHORT).show();

            }

        });

    }

}






















阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 睡觉落枕脖子疼怎么办 心胸狭窄的人怎么办 备孕期有炎症怎么办 我压里塞东西了怎么办 虫子钻进皮肤里怎么办 肚子里有寄生虫怎么办 新生儿腹泻拉水怎么办 红线虫钻进尿道怎么办 ps打开程序错误怎么办 喝酒肚子就大怎么办 肚子上的肉很软怎么办 肚子上有肥肉怎么办 孕晚期肚皮痒怎么办 妈妈腰疼厉害怎么办 哺乳期上班奶涨怎么办 婴儿拉水样大便怎么办 宝宝拉水样大便怎么办 宝宝生理性腹泻怎么办 孩子吃母乳拉稀怎么办 宝宝母乳性黄疸怎么办 宝宝12天拉肚子怎么办 吃母乳会拉肚子怎么办 哺乳期牙痛脸肿怎么办 母乳期牙龈发炎怎么办 不喜欢去奶奶家怎么办 小孩经常肚子胀气怎么办 儿童肚子胀气痛怎么办 肚子痛肠鸣便秘怎么办 肚胀大便不出来怎么办 肚子胀又便秘怎么办 经常便秘肚子大怎么办 肚子胀大便拉不出来怎么办 风油精入眼睛里怎么办 隔奶乳房胀痛怎么办 宝宝戒奶奶胀痛怎么办 隔奶奶不会涨怎么办 显示应用未安装怎么办 装修公司拖延工期怎么办 卧室比客厅大怎么办 客厅小卧室大怎么办 旱厕改造粪便怎么办