Android的简单控件

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

TextView

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

<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);

EditText

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

<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();

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 CheckBox

  • RadioButton CheckBox为单选按钮,它需要与RadioGroup配合使用,提供两个或多个互斥的选项集。
  • RadioGroup是单选组合框,可容纳多个RadioButton,并把他们组合在一起,实现单选状态。
  • 利用setOnCheckedChangeListener()监听RadioGroup控件状态,通过if语句判断被选中RadioButton的id
  • CheckBox为多选按钮,允许用户同时选中一个或多个选项;
  • 用法与RadioButton类似,有checked属性。

ImageView

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

发布了10 篇原创文章 · 获赞 41 · 访问量 5927

猜你喜欢

转载自blog.csdn.net/qq_44164791/article/details/104252517