Android的简单控件

控件是界面组成的主要元素,是与用户进行直接交互的。

在这里插入图片描述

TextView
  • TextView是用于显示文字(字符串)的控件,可在代码中通过设置属性改变文字的大小、颜色、样式等。
 <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#D81B60"
android:textSize="26sp"
/>
  • 在Java中也可以设置TextView的属性
textView.setText("Hello World!");
int color =
this.getResources().getColor(R.color.colorAccent);
textView.setTextColor(color);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);

EditText

  • EditText是可以进行编辑操作的文本框,将用户信息传递给Android程序。还可以为EditText控件设置监听器,用来测试用户输入的内容是否合法。
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="请输入用户名"
/>
  • Java中设置EditText的属性
String string =
editText.getText().toString();

Button

  • Button是按钮,是用于响应用户的一系列点击事件,使程序更加流畅和
    完整。
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="click"/>
  • 点击事件实现方式—匿名内部类
    1.匿名内部类方式
    2.在Activity中添加匿名内部类

点击Button后执行onClick里的语句

button.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		Log.i("匿名内部类方式", "button is clicked");
	}
});

RadioButton

  • RadioButton为单选按钮,它需要与RadioGroup配合使用,提供两个或
    多个互斥的选项集。
  • RadioGroup是单选组合框,可容纳多个RadioButton,并把它们组合在
    一起,实现单选状态。

checkedtrue的时候就是被选中状态

<RadioGroup
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“match_parent” >
<RadioButton
android:id=“@+id/radioButton3”
android:checked="true"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“男” />
<RadioButton
android:id=“@+id/radioButton4”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“女" />
</RadioGroup>
  • 在Java中判断被选中的状态

通过setOnCheckedChangeListener来监听RadioGroup控件状态,通过
if语句判断被选中RadioButton的id。

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId == R.id.radioButton3) {
			textView.setText("您的性别是:男");
		} else {
			textView.setText("您的性别是:女");
		}
	}
});

CheckBox

  • CheckBox为多选按钮,允许用户同时选中一个或多个选项;
  • 用法与RadioButton类似,有checked属性。

setOnCheckedChangeListener同上

<CheckBox
android:id="@+id/checkBox2"
android:checked="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox" />

ImageView

  • ImageView是视图控件,它继承自View,其功能是在屏幕中显示图像。
    ImageView类可以从各种来源加载图像(如资源库或网络),并提供缩
    放、裁剪、着色(渲染)等功能。
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/backgrounds/scenic[0]"
/>
  • Java中通过setImageResource的方法来设置Image的图片
imageView.setImageResource(R.drawable.ic_launcher_foreground);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值