4th Section Button
A button is a control that needs to interact with the user.
Button
Inherit from TextView
, whatever is TextView
specific, it has.
publicclass Button extends TextView { ......}
4.1 and
TextView
The different
Button
A special style is used TextView
. In the Button
process of creation, it uses a style inside the system,
publicButton(Context context, AttributeSet attrs) { //创建Button的时候,系统給它使用了特别的style:com.android.internal.R.attr.buttonStyle this(context, attrs, com.android.internal.R.attr.buttonStyle);}
4.2
Button
How to use
Button
After being clicked, you need to respond. Button
in fact, all View
(including TextView
) can add a response.
Android offers two ways to respond.
4.2.1 Setting a response in an XML layout file
To Button
add an attribute Android:onclick, let its value be equal to the function name of the listener function. For example, call the onclick
<Button ...... <!--设置监听函数onClick()--> android:onClick="onClick"/>
In Java source code, create a onClick()
function, declare public
it, return void
type, pass in Parameters View
,
publicvoidonClick(View v){}
Use View
getId()
, identify the object that needs to be responded to, add a response code,
publicvoidonClick(View v){ //使用getId(),识别需要响应的对象 switch(v.getId()) { //添加需要响应的操作代码; case R.id.button1: break; } break;}
4.2.2 Setting the response in Java source code
-
In Java source code, create listener,
onclicklistener Mlistener = new Onclicklistener () {//this onclick () The function is exactly the same as defined in 4.2.1 public void onclick (View v) {//use GetID () to identify objects that need to be responded to switch (V.getid ()) {//Add the action code that needs to be responded to; } break ; }}
findViewById()
Button
View
The setOnClickListener
set-up listener mListener
that passes through;
//获取xml中的Button对象Button btn = (Button) findViewById(R.id.my_button);//设置监听器btn.setOnClickListener( mListener );
Although this is the Button
response, in onClick
fact, all View
can respond to click action.
All controls or layouts are View
subclasses, so they are set to listen the same way.
4.3 Button click Effect
In addition to using system-designed button styles, you can define your own buttons to press down and not press down.
On the res\drawable\
directory, right-click to start the wizard to create drawable resource;
Create a selector drawable XML file with the file name button_selector
;
In this way, res\drawable\
a drawable file named is created in the directory button_selector.xml
.
Depending on Button
whether they are pressed android:state_pressed
, different drawable are set for each of them.
android:state_pressed=true
, indicating that the current button is pressed,
android:state_pressed=false
, indicating that the current button is not pressed;
Here we use the ColorDrawable
demo to set the drawable. The res\values\colors.xml
name of the color defined in; Set the color for the android:drawable
property,
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@color/colorBtnNormal"/> <item android:state_pressed="true" android:drawable="@color/colorBtnPressed"/></selector>
If you use other types of drawable, you are using similar methods.
For example, the background is a picture: Press use normal.png
, not pressed using pressed.png
. These pictures are placed under the res\mimap\
directory. The corresponding selector should be written,
< Selector xmlns:android = "HTTP +/ Schemas.android.com/apk/res/android "; <item android:state_pressed = "false" android:drawable = "@mipmap/normal" /> Span class= "Hljs-tag" ><item android:state_ Pressed = "true" android:drawable =< Span class= "Hljs-value" > "@mipmap/pressed" /> </ selector ;
Give Button
the android:background
attribute, using drawable selector,
<Button......android:background="@drawable/button_selector"/>
Layout and Control (iv) Response and background of-button