Button is a very simple control in Android. In our usual projects, it can be said that Button is very common and its usage rate is quite high. In itself, the use of this control is also well grasped. Next, we will introduce its use from several aspects, as well as the use of some custom Buttons.
First of all, I will talk about some basic uses of Button.
<!--Create a button and name it btn1-->
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Button 1"
/>
<!--There's nothing special about it. We just use it."android:background"Add a red color to the button.-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"
android:background="#ff0000"
/>
////Here we just use"android:background"Add an image background for the button (I prepared it beforehand, placed it in the mipmap Next)
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Picture button"
android:background="@mipmap/blue"
/>
Use point 9 pictures:
Generally speaking, we have an SDK when we are android studio, and the default path of the point 9 tool is "SDK - tools - draw9patch.bat", which is our point 9 tool. Of course, we can also configure it directly into android studio, so that we can open it directly in as. It is also possible to open it directly under the path just taken. Or if we can't find draw9patch.bat in this directory, we might as well just create a new image, rename it in. 9.XX format, drag it directly to our project, or trigger this tool. So how do I use the point 9 tool?
Point 9 Pictures can be directly quoted after processing!
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Point 9 Picture Button"
android:background="@mipmap/blue_deal"
/>
Use shape to Customize button shape:
New btn_shape.xml under drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/><!--Set up rounded corners-->
<solid android:color="@color/grey"/><!--Set the background color-->
</shape>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Round corner picture"
android:background="@drawable/btn_shape"
/>
Use custom styles under style:
<!--General Style-->
<style name="btnStyle">
<item name="android:background">@drawable/btn_shape</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">22sp</item>
</style>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="General Style"
style="@style/btnStyle"
/>
selector to use button selection changes:
New btn_selector.xml under drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--Button Down Style-->
<item android:state_pressed="true" >
<shape>
<corners android:radius="10dp"></corners>
<solid android:color="@color/red"></solid>
<stroke android:width="2dp" android:color="#FFC626"></stroke>
</shape>
</item>
<!--Button default style-->
<item>
<shape>
<corners android:radius="10dp"></corners>
<solid android:color="@color/black"></solid>
<stroke android:width="2dp" android:color="#FFC626"></stroke>
</shape>
</item>
</selector>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Change the button background"
android:textColor="#ffffff"
android:background="@drawable/btn_selector"
/>
Let's talk about some button listening events (two commonly used ones):
First: By implementing OnClickListener interface
public class MainActivity extends Activity implements View.OnClickListener {
//Implementing OnClickListener Interface
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
//Find Button, because it's the VIEW that's returned, so let's make a strong turn.
Button btn = (Button) findViewById(R.id.btn);
//Binding listening
btn.setOnClickListener(this);
}
//Rewrite the onClick() method
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
}
Second: Use anonymous internal classes
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
Button btn = (Button) findViewById(R.id.btn);
//Using anonymous inner classes
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
Specific use of which to see personal preferences!