前言
组件是 Android 程序设计的基本组成单元,通过使用组件可以高效地开发 Android 应用程序。
文本类组件
Android 中提供了一些与文本显示、输入相关的组件,通过这些组件可以显示或输入文字。
TextView 类:用于显示文本的组件为文本框组件。
EditText 类:允许用户编辑文本内容。
举例说明:编辑框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#EAEAEA"
tools:context=".MainActivity">
<!-- 添加写说说编辑框 -->
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#FFFFFF"
android:gravity="top"
android:hint="说点什么吧..."
android:inputType="textMultiLine"
android:lines="6"
android:padding="5dp"></EditText>
<ImageView
android:id="@+id/picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/edit"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:src="@drawable/addpicture" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加照片"
android:layout_alignTop="@+id/picture"
android:lines="5"
android:layout_toRightOf="@+id/picture"
android:padding="8dp"
android:background="#FFFFFF"
android:textColor="#767676"
/>
</RelativeLayout>
结果
按钮类组件
在 Android 中,提供了一些按钮类的组件,主要包含普通按钮、图片按钮、单选按钮和复选框。
Button类:普通按钮,用于触发一个指定的事件。
ImageButton类:图片按钮,用于触发一个指定的事件。
RadioButton类:单选按钮,一组仅有一个可被选中。
CheckBox类:复选框。一组中可同时选中多个。
举例说明:单选按钮
public class MainActivity extends AppCompatActivity {
Button bt; //定义提交按钮
RadioGroup rg; //定义单选按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt); //通过ID获得布局提交按钮
rg = (RadioGroup) findViewById(R.id.rg); //通过ID获得布局单选按钮组
//练习:为A选项设置单击事件监听器
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton rb_a = (RadioButton) findViewById(R.id.rb_a);
if(rb_a.isChecked()){
Toast.makeText(MainActivity.this, "目前选项为A", Toast.LENGTH_LONG).show();
}
}
});
//为提交按钮设置单击事件监听器
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0; i<rg.getChildCount(); i++){
//根据索引值获取单选按钮
RadioButton radioButton = (RadioButton) rg.getChildAt(i);
//判断单选是否被选中
if(radioButton.isChecked()){
//如果答案正确
if(radioButton.getText().equals("B:2")){
Toast.makeText(MainActivity.this,"回答正确", Toast.LENGTH_LONG).show();
}
//否则
else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("回答错误,下面请看解析:由于苹果和梨是不同的两种水果,则小明一共有两种水果。");
//单击确定提示框消失
builder.setPositiveButton("确定",null).show();
}
break; //跳出 for
}
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小明有一个苹果和一个梨,问小明有多少种水果?"
android:textSize="25dp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:layout_below="@+id/text1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb_a"
android:textSize="20dp"
android:text="A:1"
/>
<RadioButton
android:id="@+id/rb_b"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B:2"
/>
<RadioButton
android:id="@+id/rb_c"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C:3"
/>
</RadioGroup>
<!-- 提交按钮 -->
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="提交"
android:textSize="20dp"
android:layout_below="@+id/rg"
/>
</RelativeLayout>
结果