1、TextView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--设置字号为20pt,在文本框结尾处绘制图片--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我爱java" android:textSize="20pt" android:drawableEnd="@drawable/ic_launcher" android:drawableRight="@drawable/ic_launcher" /> <!--设置中间省略,所有字母大写--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:text="我爱java我爱java我爱java我爱java我爱java我aaajava" android:ellipsize="middle" android:textAllCaps="true"/> <!--对邮件、电话增加链接--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:text="邮件是kongyeeku@163.com电话是020888888888" android:autoLink="email|phone"/> <!--设置文字颜色、大小,并使用阴影--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试文字" android:shadowColor="#00f" android:shadowDx="10.0" android:shadowDy="8.0" android:shadowRadius="3.0" android:textColor="#f00" android:textSize="18pt"/> <!--测试密码框--> <TextView android:id="@+id/passwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello" android:password="true"/> <CheckedTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="可勾选的文本" android:checkMark="@drawable/ok"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--通过android:background指定背景--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="带边框的文本" android:textSize="24pt" android:background="@drawable/bg_border"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="圆角边框、渐变背景的文本" android:textSize="24pt" android:background="@drawable/bg_border2"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--设置背景色为透明色--> <solid android:color="#0000"/> <!--设置红色边框--> <stroke android:width="4px" android:color="#f00"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--指定圆角矩形的4个圆角的半径--> <corners android:topLeftRadius="20px" android:topRightRadius="5px" android:bottomRightRadius="20px" android:bottomLeftRadius="5px"/> <!--指定边框线条的宽度和颜色--> <stroke android:width="4px" android:color="#f0f"/> <!--指定使用渐变背景色,使用sweep类型的渐变 颜色从红色->绿色->蓝色--> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"/> </shape>
2、EditView
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1"> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名:" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写登录账号" android:selectAllOnFocus="true"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码:" android:textSize="16sp"/> <!--android:inputType="numberPassword"表明只能接受数字密码--> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberPassword"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="年龄" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生日:" android:textSize="16sp"/> <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:inputType="date"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电话号码" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写您的电话号码" android:selectAllOnFocus="true" android:inputType="phone"/> </TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册"/> </TableLayout>
3、Button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!--文字带阴影的按钮--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文字带阴影的按钮" android:textSize="12pt" android:shadowColor="#aa5" android:shadowRadius="1" android:shadowDx="5" android:shadowDy="5"/> <!--普通文字按钮--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/red" android:text="普通按钮" android:textSize="10pt"/> <!--带文字的图片按钮--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_selector" android:textSize="11px" android:text="带文字的图片按钮"/> </LinearLayout>
4、复选框和单选钮
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"/>
<!--定义一组单选钮-->
<RadioGroup android:id="@+id/rg"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<!--定义两个单选钮-->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:"/>
<!--定义一个垂直的线性布局-->
<LinearLayout android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<!--定义三个复选框-->
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:checked="true"/>
<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>
</TableRow>
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableLayout>
package com.example.myapplication8; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends AppCompatActivity { RadioGroup rg; TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取界面上rg、show两个组件 rg=(RadioGroup)findViewById(R.id.rg); show=(TextView)findViewById(R.id.show); //为RadioGroup组件的onCheckedChanged事件绑定事件监听器 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //根据用户勾选的单选按钮来动态改变tip字符串的值 String tip=checkedId==R.id.male? "您的性别是男人":"您的性别是女人"; //修改show组件中的文本 show.setText(tip); } }); } }
5、状态开关按钮和开关
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--定义一个ToggleButton按钮--> <ToggleButton android:id="@+id/toggle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="横向排列" android:textOn="纵向排列" android:checked="true"/> <Switch android:id="@+id/switcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="横向排列" android:textOn="纵向排列" android:checked="true" android:thumb="@drawable/check"/> <!--定义一个可以动态改变方向的线性布局--> <LinearLayout android:id="@+id/test" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮3"/> </LinearLayout> </LinearLayout>
package com.example.myapplication9; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { ToggleButton toggle; Switch switcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggle=(ToggleButton)findViewById(R.id.toggle); switcher=(Switch)findViewById(R.id.switcher); final LinearLayout test=(LinearLayout)findViewById(R.id.test); CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton button, boolean isChecked) { if (isChecked) { //设置LinearLayout垂直布局 test.setOrientation(1); toggle.setChecked(true); switcher.setChecked(true); } else { //设置LinearLayout水平布局 test.setOrientation(0); toggle.setChecked(false); switcher.setChecked(false); } } }; toggle.setOnCheckedChangeListener(listener); switcher.setOnCheckedChangeListener(listener); } }
6、时钟
7、计时器
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Chronometer android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
package com.example.myapplication11; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.widget.Button; import android.widget.Chronometer; public class MainActivity extends AppCompatActivity { Chronometer ch; Button start; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取计时器组件 ch=(Chronometer)findViewById(R.id.test); //获取“开始”按钮 start=(Button)findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View source) { //设置开始计时时间 ch.setBase(SystemClock.elapsedRealtime()); //启动计时器 ch.start(); start.setEnabled(false); } }); //为Chronometer绑定事件监听器 ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { //如果从开始计时到现在超过了20s if(SystemClock.elapsedRealtime()-ch.getBase()>20*1000) { ch.stop(); start.setEnabled(true); } } }); } }