Button controls are a subclass of TextView, so most of the properties of TextView can also be used in Button. The difference is that we can set the background property of Button to StateListDrawable, which allows buttons to display different images in different states.
StateListDrawable
StateListDrawable is also a Drawable resource. It can set different picture effects according to different states. The root node is <selector>, and the common properties are as follows:
- Drawable: Referenced Drawable bitmap, we can put it first to indicate the normal state of the component~
- State_Focus:Get focus or not
- State_Window_Focus: Get window focus or not
- state_enabled: Is the control available
- state_checkable: whether the control can be checked, eg:checkbox
- state_checked: Is the control checked
- State_Select: Whether the control is selected or not, for cases with rollers
- state_pressed: Is the control pressed
- state_active: Is the control active, eg:slidingTab
- state_single: Determines if only one child control is displayed when the control contains more than one child control
- state_first: Determines whether the first child control is in the display state when the control contains more than one child control
- state_middle: Determines if the middle child control is in the display state when the control contains more than one child control
- state_last: Determines if the last child control is in the display state when the control contains more than one child control
Example 1
Use the StateListDrawable implementation button in the LinearLayout container to press Change Colors and Pictures as follows:
1. Create a new empty project, right-click the Drawable directory, New Drawable Resource File, named btn_color_selector, modify its code as follows:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--Press status shows red--> <item android:color="#ffff0000" android:state_pressed="true" /> <!--Blue by default--> <item android:color="#ff0000ff" /> </selector>
2. Right-click the Drawable directory, New Drawable Resource File, named btn_selector, modify its code as follows:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--Press Status Display ic_baseline_airplanemode_inactive_24 picture--> <item android:drawable="@drawable/ic_baseline_airplanemode_inactive_24" android:state_pressed="true"/> <!--Default status display ic_baseline_airplanemode_active_24 picture--> <item android:drawable="@drawable/ic_baseline_airplanemode_active_24"/> </selector>
3. Modify activity_The main.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <Button android:text="Change!!!" android:background="@drawable/btn_selector" android:backgroundTint="@drawable/btn_color_selector" android:layout_width="200dp" android:layout_height="100dp"/> </LinearLayout>
4. If the API version used by your project is more than 29, you need to modify the themes.xml file in values and change the parent property of <style>to:
Theme.MaterialComponents.DayNight.DarkActionBar.Bridge
5. Start the project on the virtual machine to see the effect
Button Common Event Handling
Button's common events in applications are OnClick, OnLongClick, OnTouch, and we can set corresponding listeners in the buttons to handle the events OnClickListener, OnLongClickListener, OnTouchListener.They are all internal classes of View, as follows:
public interface OnClickListener { void onClick(View var1); } public interface OnLongClickListener { boolean onLongClick(View var1); } public interface OnTouchListener { boolean onTouch(View var1, MotionEvent var2); }
So these internal interfaces are all function interfaces, so we can use the new feature of Java8--Lamba expression to simplify programming in development!
When onLongClick or onTouch returns true, it indicates that the event has been consumed and will not respond to the remaining listener methods.
The onTouch event includes clicks, moves, and so on. We can call MotionEvent's getAction() method to see the specific touch event.
Example 2
The test implements button click, long press, touch event handling as follows:
1. Create a new empty project and modify the activity_The main.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <Button android:id="@+id/btn" android:text="Button event handling" android:background="#ff0000ff" android:layout_width="200dp" android:layout_height="100dp"/> </LinearLayout>
2. Modify MainActivity.java as follows
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn); //Click Event Processing btn.setOnClickListener((v)->{ Log.e("Liu","OnClick"); }); //Long by Event Processing btn.setOnLongClickListener((v)->{ Log.e("Liu","OnLongClick"); return false; }); //Touch event handling btn.setOnTouchListener((v,e)->{ Log.e("Liu","OnTouch "+e.getAction()); return false; }); } }
3. Run on the virtual machine and view the logs on the console!Modify the return values of onLongClick event handling and OnTouch event handling to view logs on the Compare Console versus other situations!