Android a common interface controls

Man of few words said, first on the map

TextView: TextView control to display text information.

Layout Properties
Functional Description
android:layout_width
Set TextView width of the control
android:layout_height
Set TextView height of the control
android:id
Set TextView controls the unique identification
android:background
Set TextView background control
android:layout_margin
Set the distance of the current control or border around the screen controls, layout
android:padding
Set TextView distance control with the control content
android:text
Setting text
android:textColor
Set the text color display
android:textSize
Set the text size, the unit is recommended sp
android:gravity
Set the text position
android:maxLength
Set the maximum length of the text exceeds the length of this text is not displayed
android:lines
The text lines of text number exceeds the number of this line is not displayed
android: maxLines
The maximum number of lines of text, beyond this number of lines of text is not displayed.
android:ellipsize
When the text is provided outside the TextView predetermined range are displayed.
android:drawableTop
In the top of the text display image
android:lineSpacingExtra
Set line spacing of text
android:textStyle
Text style setting, such as Bold (bold), Italic (italics), Normal (normal)
Example:

<TextView

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android: text = "text message TextView display!"

    android:textSize="25dp"

    android:textColor="#FF79E3"

    android:gravity="center_horizontal"

    android:textStyle="italic"

    />

operation result:
 

 Button: Button control represents a button that inherits from TextView control, not only can display text and can display images, but also allows the user to perform operations by clicking, when the Button control is clicked, there will be press and pop background a dynamic transition effects, this effect is click effect.

Button OnClick of implementation:

  • The Button control in the layout file attribute onClick way

    ① specify the onClick attribute in the layout file: android: onClick = "click"

    ② click method to achieve this in an Activity:

   public void click(View v){

        Log.i ( "onClick attribute specified mode", "button is clicked");

     }

  • Anonymous inner class way

    Add in the interior anonymous Activity

    btn.setOnClickListener(new View.OnClickListener() {

        @Override

         public void onClick(View v) {

             Log.i ( "anonymous inner classes embodiment", "button is clicked");

         }

      });

  • Interface

    ① Button control click listener event: btn.setOnClickListener (this);

    ② Then current Activity realize OnClickListener interfaces:
    public class MainActivity the extends AppCompatActivity the implements View.OnClickListener

    ③ Then OnClickListener interface implementation:    
    @Override public void the onClick (View V) {Log.i ( "Interface", "button is clicked") ;}

Example:
MainActivity.java

package com.example.controlcomponentdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button1;

    private Button button2;

    private Button button3;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);

        button2 = findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                button2.setText ( "two button was clicked");

            }

        });

        button3 = findViewById(R.id.button3);

        button3.setOnClickListener(this);

    }

    public void click(View view) {

        button1.setText ( "a button was clicked");

    }

    @Override

    public void onClick(View view) {

        button3.setText ( "Button3 was clicked");

    }

    

}

activty_main.xml

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android: text = "implementation of a"

    android:id="@+id/button1"

    android:onClick="click"

>

</Button>

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android: text = "implementation of two"

    android:id="@+id/button2"

    >

</Button>

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android: text = "implementation of three"

    android:id="@+id/button3"

    >

</Button>

 operation result:

 EditText: EditText represents an edit box, which is a subclass of TextView user may input information in this control.

 Property Name
Functional Description
android:hint
Control is empty when displaying the prompt text messages
android:textColorHint
Control content is empty display of color prompt text information
android:password
Enter in the text box is displayed as " . "
android:phoneNumber
Set the text box's contents can only be a number
android: MinLines
The minimum number of lines of text
android:scrollHorizontally
Setting text information exceeds EditText the width of the case, whether there is cross brace
android:editable
Is set editable
Example:

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginTop="140dp"

    android:orientation="vertical">

 

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="姓名:"

        android:textSize="28sp"

        android:textColor="#000000"

        android:textStyle="italic"

        ></TextView>

    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android: hint = "Please enter your name."

        android:maxLines="2"

        android:textColor="#000000"

        android:textSize="20sp"

    ></EditText>

</LinearLayout>

operation result:
 

 ImageView: ImageView represent picture, it inherits from View, you can load a variety of image resources.

Property Name
Functional Description
android:layout_width
Set ImageView width of the control
android:layout_height
Set ImageView height of the control
android:id
Set ImageView controls the unique identification
android:background
Set ImageView background control
android:layout_margin
Set the distance of the current control or border around the screen controls
android:src
Set ImageView image resources controls to be displayed
Android: scaleType
Picture zoom or move the resources to accommodate ImageView control the width and height
android:tint
The picture rendering to the specified color
Example:

<RelativeLayout

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginTop="230dp">

    <ImageView

        android:layout_width="match_parent"

        android:layout_height="400dp"

        android:background="@drawable/bg"

        ></ImageView>

    <ImageView

        android:layout_width="100dp"

        android:layout_height="100dp"

        android:src="@drawable/fo"

        ></ImageView>

</RelativeLayout>

运行结果:
 

RadioButton:RadioButton为单选按钮,android:checked属性指定是否选中的状态。

RadioGroup是单选组合框,可容纳多个RadioButton,并把它们组合在一起,实现单选状态。

示例:
avtivty_main.xml

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginTop="430dp"

    android:orientation="horizontal">

    <RadioButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/man"

        android:text="男"

        android:textSize="28sp"

        ></RadioButton>

    <RadioButton

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/woman"

        android:text="女"

        android:textSize="28sp"

        ></RadioButton>

    <TextView

        android:id="@+id/gendel"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="你选择的性别是:"

        ></TextView>

</LinearLayout>

MainActivty.java

package com.example.controlcomponentdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RadioButton radioButton_man;

    private RadioButton radioButton_woman;

    private TextView textView;

        radioButton_man = findViewById(R.id.man);

        radioButton_woman = findViewById(R.id.woman);

        textView = findViewById(R.id.gendel);

        radioButton_man.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                textView.setText("您选择的性别是男!");

            }

        });

        radioButton_woman.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                textView.setText("您选择的性别是女!");

            }

        });

    }

}

运行结果:
 

CheckBox表示复选框,它是Button的子类,用于实现多选功能,通过android:checked属性指定CheckBox控件是否选中的状态。

示例:

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_marginTop="460dp">

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="请选择爱好:"

        android:textSize="20sp"

        >

    </TextView>

    <CheckBox

        android:id="@+id/bassketball"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="篮球">

    </CheckBox>

    <CheckBox

        android:id="@+id/football"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="足球">

    </CheckBox>

    <CheckBox

        android:id="@+id/tennis"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="网球">

    </CheckBox>

</LinearLayout>

运行结果:
 

 Toast是Android系统提供的轻量级信息提醒机制,用于向用户提示即时消息,它显示在应用程序界面的最上层,显示一段时间后自动消失不会打断当前操作,也不获得焦点。
 

public class MainActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main)

        Toast.makeText(this,"Wifi已断开",Toast.LENGTH_LONG).show();

     }

}

运行结果:
 
 
 
 
 
 
 
发布了40 篇原创文章 · 获赞 2 · 访问量 5185

Guess you like

Origin blog.csdn.net/Dnesity/article/details/104612787