Android的简单控件

控件时界面组成的主要元素,是与用户进行直接交互的。
简单控件常用的有:
TextView、EditText、Button、RadioButton、CheckBox、ImageView

TextView

TextView是用于显示文字(字符串)的控件,可在代码中通过设置属性改变文字大小、颜色样式等。
例:HelloWorld!

<TextView
	android:id = "@+id/textView"
	android:layout_width = "wrap_content"
	android:layout_height = "wrap_content"
	android:text = "HelloWorld!"
	android:textColor = "#D81B60"
	android:textSize = "26sp"
/>

java中代码为

textView.setText("HelloWorld!");
int color = this.getResources().getColor(R.color.colorAccent);
textView.setTextColor(color);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);

TextView常用属性
在这里插入图片描述

EditText

EditText继承自TextView,可以进行编辑操作的文本框,将用户信息传递给Android程序。还可以为EditText控件设置监听器,用来测试用户输入内容是否合法。

<TextView
 android:id = "@+id/editText"
 android:layout_width = "wrap_content"
 android:layout_height = "wrap_content"
 android:inputType = "textPersonName"
 android:hint = "请输入用户名"
/>

java中代码为

String string = editText.getText().toString();

EditText常用属性
在这里插入图片描述

Button

Button是按钮,是用于相应用户的一系列点击事件,使程序更加流畅和完整。
android studio中:

<TextView
 android:id = "@+id/button"
 android:layout_width = "wrap_content"
 android:layout_height = "wrap_content"
 android:text = "Buton"
 android:textColor = "click"
/>

点击事件实现方式–匿名内部类
匿名内部类方式
在Acivity中添加匿名内部类

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

RadioButton

  • RadioButton CheckBox为单选按钮,它需要与RadioGroup配合使用,提供两个或多个互斥的选项集。
  • RadioGroup是单选组合框,可容纳多个RadioButton,并把他们组合在一起,实现单选状态。
    例:选男女性别。
<RadioGroup
    android:orientation = "vertical"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent">
    <RadioButton
    	 android:id = "@+id/radioButton3"
    	 android:checked = "ture"
 	 android:layout_width = "mach_parent"
 	 android:layout_height = "wrap_content"
 	 android:text = "男"/>
    <RadioButton
      android:id = "@+id/radioButton4"
      android:checked = "ture" 
      android:layout_width = "mach_parent"
      android:layout_height = "wrap_content"
      android:text = "女"/> 
</RadioGroup>
  • 利用setOnCheckedChangeListener()监听RadioGroup控件状态,通过if语句判断被选中RadioButton的id
radioGroup.setOnCheckedChangeListener(new RadioGroup.setOnCheckedChangeListener(){
   @Override
   public void setOnCheckedChange(Redio Group, int checkedId){
   	if (checkedId == R.id.radioButton3){
   		textView.setText("您的性别是:男");
   	}else{
   		textView.setText("您的性别是:女");
   	}
   }
});

CheckBox

  • CheckBox为多选按钮,允许用户同时选中一个或多个选项;
  • 用法与RadioButton类似,有checked属性。
    <CheckBox
      android:id = "@+id/checkBox2"
      android:checked = "ture" 
      android:layout_width = "mach_parent"
      android:layout_height = "wrap_content"
      android:text = "CheckBox"/> 

ImageView

ImageView是视图控件,它继承来自View,qigongneng是在屏幕中显示图像。ImageView类可以从各种来源加载图像(如资源库或网络),并提供缩放、裁剪、着色(渲染)等功能。

    <ImageView
      android:id = "@+id/imageView2"
      android:layout_width = "mach_parent"
      android:layout_height = "wrap_content"
      tools:srcCompat = "@tools:sample/backgrounds/scenic[0]"/>
imageView.setOImageResource(R.drawable.ic_launcher_foreground);

Android的Activity(活动)https://blog.csdn.net/qq_44164791/article/details/104278506

发布了11 篇原创文章 · 获赞 43 · 访问量 8473
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章