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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章