Android控件

文本框标签 

layout_width 控件的宽度     layout_height  控件的高度

fill_parent 随着控件的高度/宽度  wrap_content 随着文字的高度/宽度

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="显示控件上要显示的内容"
    />

1.Activity常用方法 

setContentView(R.layout.xxxx);//设置布局文件 

findViewById(R.id.xxxx)//获取控件 

getString(R.string.xxxx)//获取strings.xml指定的key的值

 注意:可以用Java代码生成界面,也可以用xml编写界面,建议用xml编写

2.1 TextView控件属性     

android:text="显示控件上要显示的内容"//显示文本     

android:textColor="#fef"//文本颜色     

android:textSize="50px"//文本字体大小    

android:maxLength="99"//文本框里文本长度为99个字符长     

android:textStyle="italic"//文本样式italic倾斜bold加粗normal正常     

android:password="true"//是否用密文显示     

android:layout_width="fill_parent"//文本框的宽     

android:layout_height="wrap_content"//文本框的高     

android:visibility="invisible"//文本框的显示  invisible 不显示 visible 显示 gone     

android:background="背景图片的地址(一般是sre资源库里的)"     

EditText(输入框)

android:selectAllOnFocus="true"//选中后全选

android:focusable="true"//获取焦点  true默认获得焦点 false不获得焦点

android:enabled="true"//是否能编辑 true不能编辑 false能编辑

  2.2 方法

    setText(String);//设置文本内容    

    getText().toString();//返回文本内容

3 RadioButton(单选按钮)  

  3.1 RadioGroup   

    android:orientation="horizontal"   

    android:checkedButton="单选按钮"

注意:单选一般要嵌套在RadioGroup中

4 CheckBox(复选框)  

  4.1属性(大部分可以用上面的)   

  android:checked="true" //默认选中  

  4.2方法   

  setChecked();

  java中代码

    CheckBox di = (CheckBox)findViewById(R.id.dd);//获取复选框控件

        di.setChecked(true);//是否给默认属性(true真的给默认选中,false假不选中)

  xml中代码

    <CheckBox

          android:id="@+id/dd"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"

          android:text="店小二"

          android:checked="true"/>

5 ImageView(图片视图)  

  5.1属性(xml代码)  

    <ImageView

         android:id="@+id/img"

         android:src="@drawable/icon"

         android:maxWidth="100px"//图片最大宽度

     android:maxHeight="50px"//图片最大高度

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"      />

注意:每个控件必须要有layout_width layout_height

6 ImageButton(图片按钮xml代码)

  <ImageButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/icon1"//调用src中drawable文件资源     />

7.TimePicker(时间控件)  

  7.1属性(xml代码)  

    <TimePicker

         android:id="@+id/tp1"

         android:layout_width="fill_parent"

         android:layout_height="wrap_content"    />  

  7.2方法(java代码)

     TimePicker time = (TimePicker)findViewById(R.id.tp1);//获取时间控件 

         int hour = time.getCurrentHour();//时

         int min = time.getCurrentMinute();//分

     time.setIs24HourView(true);//设置时间显示24小时制

         time.setCurrentHour(18);//设置时为18点

         time.setCurrentMinute(30);//设置分为30分

8 DatePicker(日期控件)

    8.1属性(xml代码)

     <DatePicker

         android:id="@+id/DP1"

         android:layout_width="fill_parent"

         android:layout_height="wrap_content"      />

    8.2方法(java代码)

    int getYear()
    int getMonth()
    int getDayOfMonth()
    setEnabled(boolean enabled)
    DatePicker DP = (DatePicker)findViewById(R.id.DP1);//获取日期控件
           DP.updateDate(2020, 11, 30);//修改日期

原文地址:https://www.cnblogs.com/luocixin/p/6836281.html