Android基本控件简介

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();

            }

        });

    }

}






















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值