Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Button controls in UI developed by Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares to you is about how to understand the Button control in Android development UI, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Preface

Recently, I have been talking about the use of the androidUI control, and this article talks about the Button control, which is basically one of the most widely used controls. Programmers who have experience in developing other platforms are no stranger to buttons. First of all, we will explain the common events and event binding and trigger of the Button control of Android, and then realize the mixed arrangement of picture and text by setting property values in the Button control, which is commonly used in the project.

Button control

Button inherits TextView. Its function is to provide a button, this button can be clicked by the user, when the user operates on the button, trigger the corresponding events, such as click, touch.

There is also an ImageButton, which inherits from Button, can display a picture in ImageButton to show the user, and its Text property setting value is invalid, other functions are the same as Button.

Common events

Generally speaking, for a button, the most commonly used is the click event, Button inherits indirectly from View, and all events in AndroidUI are defined in View. In this blog, the examples show click events, touch events, and other events in a similar way, except that they are triggered at different times. Here, you need to implement the methods of View.OnClickListener and View.OnTouchListener interfaces respectively.

View.OnClickListener, you need to implement the onClick (View v) method, where v is the control that currently triggers the event.

View.OnTouchListener, need to implement onTouch (View v, MotionEvent event), where v is the current trigger event control, event includes the specific content of the touch, such as moving, pressing, and so on.

The following use an example to explain the event binding and trigger, in the example shows two button controls, one for the ordinary button, a button to fill the picture, bind the click event for them, when the click event triggered, modify its size, bind the picture button to touch the event, trigger when touched, switch the picture display.

Layout code:

Implementation code:

Package com.bgxt.buttondemo; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.Button; / / support public class ButtonListener extends Activity implements OnClickListener for click and touch events by implementing the interface, OnTouchListener {private Button btnChangeSize; private Button btnChangeImg; private int flag = 1 @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.btn_listener); btnChangeSize = (Button) findViewById (R.id.btnChangeSize); btnChangeImg = (Button) findViewById (R.id.btnChangeImg); / / event binding btnChangeSize.setOnClickListener (this) to two buttons; btnChangeImg.setOnClickListener (this); btnChangeImg.setOnTouchListener (this) } @ Override public boolean onTouch (View v, MotionEvent event) {/ / get the Button control that triggers the event Button btn = (Button) v; if (event.getAction () = = MotionEvent.ACTION_UP) {/ / when pressed on touch, replace the display picture with image1 btn.setBackgroundResource (R.drawable.image1) } else {btn.setBackgroundResource (R.drawable.image2);} return false;} @ Override public void onClick (View v) {Button btn = (Button) v If (flag = = 1 & & btn.getWidth () = = getWindowManager (). GetDefaultDisplay () .getWidth ()) {/ / if equal to the width of the screen, modify the logo flag to-1 flag =-1;} else if (flag = =-1 & & btn.getWidth ())

< 100) { flag = 1; } // 设置button控件尺寸 btn.setWidth(btn.getWidth() + (int) (btn.getWidth() * 0.1) * flag); btn.setHeight(btn.getHeight() + (int) (btn.getHeight() * 0.1) * flag); } } 展示效果图:

When the button is clicked, the button is enlarged, and when it is zoomed in to the width of the screen, it begins to zoom out.

When you touch the icon button, the image changes.

Mixed arrangement of picture and text

For the actual project, it is often necessary to set the button to show the effect of mixed arrangement of picture and text, so that the function of the button can be shown to the user more intuitively through the chart, and there can be a short text description. Although ImageButton can also achieve the effect of picture buttons, setting the Text property is not useful for ImageButton, so the use of ImageButton is not covered here. For Button controls, mixed text needs to use an android:drawableXxx property (Xxx is the direction of the button where the picture is located), this property with android:text, you can achieve the effect of mixed text.

The following example shows butcons in four directions, up and down, left and right, and generates a button that dynamically generates mixed graphics and text through Java code. Because Button inherits from TextView, setting up image-text mixing through code is similar to TextView, using the SpannableString class.

Layout code:

Java implementation code:

Package com.bgxt.buttondemo; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ImageSpan; import android.widget.Button; public class ButtonStyle extends Activity {private Button btnSty; @ Override protected void onCreate (Bundle savedInstanceState) {/ / TODO Auto-generated method stub super.onCreate (savedInstanceState) SetContentView (R.layout.btn_style); / / get button control btnSty= (Button) findViewById (R.id.btnSty); / / generate SpannableString for image carrier SpannableString spannebleLeft=new SpannableString ("left"); Bitmap bitmapleft=BitmapFactory.decodeResource (getResources (), R.drawable.image1); ImageSpan imageSpanLeft=new ImageSpan (ButtonStyle.this,bitmapleft); spannebleLeft.setSpan (imageSpanLeft,0,4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) SpannableString spannebleRight=new SpannableString ("right"); Bitmap bitmapRight=BitmapFactory.decodeResource (getResources (), R.drawable.image2); ImageSpan imageSpanRight=new ImageSpan (ButtonStyle.this,bitmapRight); spannebleRight.setSpan (imageSpanRight,0,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); / / append the generated SpannableString to the button btnSty.append (spannebleLeft); btnSty.append ("aLi"); btnSty.append (spannebleRight) }}

Effect display:

For the actual project, the general style of the button will be wrapped in an additional XML style file, which will be introduced later. Here is just a brief introduction to the simple use of Button. The most useful use of buttons is for users to click to trigger the corresponding time, there is no difficulty.

The above is how to understand the Button controls in Android development UI. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report