Android development is based on the android Studio platform - the use of the control TextView, the use of the control Button, StateListDrawable StateListDrawable

1. Control TextView

1. Detailed explanation of basic attributes

1. layout _ width : Width of the component
2. layout _ height : component height
3.id: Set a component id for TextView 
4. text : Set the displayed text content
5. textColor: set font color
6.TextStyle: Set the font style, three optional values: normal (no effect), bold (bold), italic (italic)
7. textSize : font size, the unit is usually sp 
8.background: The background color of the control can be understood as the color that fills the entire control, which can be a picture
9. gravity : Set the alignment direction of the content in the control, text in TextView, pictures in ImageView, etc.

Code implementation effect:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <com.example.wzyproject.MyTextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="176dp"
        android:text="@string/tv_one"
        android:textColor="@color/black"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center_vertical"
        android:shadowDx="10.0"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"

        />
</LinearLayout>

2 TextView with shadow 

1. android : shadowColor : Set the shadow color, need to be used together with shadowRadius
2. android : shadowRadius : Set the blurring degree of the shadow. If it is set to 0.1, it will become the font color. It is recommended to use 3.0
3. android : shadowDx : Set the offset of the shadow in the horizontal direction, which is the abscissa position where the shadow in the horizontal direction starts
4.android: shadowDy : Set the offset of the shadow in the vertical direction, which is the vertical coordinate position where the shadow starts in the vertical direction

3. TextView that realizes the marquee effect 

1. android : singleLine : content single line display
2. android : focusable : Is it possible to get focus
3. android : focusableinTouchMode : Used to control whether the view can be focused in touch mode
4. android : ellipsize : where to omit text
5. android : marqueeRepeatlimit : The number of times the subtitle animation repeats
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"

It is not enough to just enter the above, the following methods are all available:

3.1 Set the click method

pass

android:clickable="true"

It can realize the effect of turning on the marquee after clicking

3.2 Customize a TextView


Create a java class and implement the constructor

package com.example.wzyproject;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

 Finally, isFocused is called, and the activity_main.xml is changed at this time

 3.3 Add requestFocus at the end to request focus

 2.butuon control

1.StateListDrawable 

StateListDrawable is a kind of Drawable resource, which can set different image effects according to different states. For the key node < selector >, we only need to set the background property of Button to this drawable resource to realize it easily. When the button is pressed, different Button color or background.

1. drawable : the referenced Drawable bitmap
2. state _ focused : Whether to get the focus
3. state _ pressed : Whether the control is pressed
4. state _ enabled : Whether the control is available
5.state_ selected : Whether the control is selected, for the case with a scroll wheel
6. state _ checked : Whether the control is checked
7. state _ checkable : Whether the control can be checked, eg: checkbox 
8. state _ window _ focused : Whether to get the window focus
9. state _ active : Whether the control is active, eg : slidingTab 
10.state_ single : When the control contains multiple child controls, determine whether only one child control is displayed
11.state_ first : When the control contains multiple child controls, determine whether the first child control is displayed
12.state_ middle : When the control contains multiple sub-controls, determine whether the middle sub-control is displayed
13.state_ last : When the control contains multiple child controls, determine whether the last child control is displayed
    <Button
        android:text="我是按钮"
        android:background="@drawable/btn_selector"
        android:layout_width="200dp"
        android:layout_height="100dp"

    />

First create two photos:

Use backgroundTint to set the color change of the button before and after clicking

 Introduced in the button block

android:backgroundTint="@color/btn_color_selector"

 The achievable effects are as follows:

 

Note: Setting the foreground color will overwrite the font!

android:foreground="#ff00ff00"

 

2. Button event processing

private static final String TAG = "led";
Button btn = findViewById(R.id.btn);

1. Click event

//点击事件
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: " );
            }
        });

2. Long press event

//长按事件
        btn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.e(TAG, "onLongClick: " );
                return false;
            }
        });

3. Touch events

//触摸事件
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e(TAG, "onTouch: " );
                return false;
            }
        });

There are three models for touch events: ACCTION_DOWN ACCTION_UP ACCTION_MOVE

 At this time, after running through the event, there will be

 When our Ontouch is True, the system will process the two methods onClick and onLongClick, which will not be called at this time.

It is equivalent to a dependency relationship, onlongclick depends on ontouch, and onclick depends on onlongclick.

when we join

android:onClick="leoClick"

 At this point leoClick replaces the above click event onClick,

    public void leoClick(View view){
        Log.e(TAG, "leoClick: " );
    }

 After clicking, the effect is as above.

Guess you like

Origin blog.csdn.net/WZY22502701/article/details/129896229