How to use the Android basic control (EditView, Seekbar, etc.) _android

Source: Internet
Author: User
Tags gettext visibility

Android provides a large number of UI controls, and this article describes how to use TextView, ImageView, Button, EditView, ProgressBar, SeekBar, ScrollView, WebView. Before introducing the various controls, let's briefly introduce the most basic properties of the Android UI control:

Id:id is a unique identifier for a control that can be manipulated by **findviewbyid (r.id.*).
Layout_width: The width of the control, which can be set to match_parent (filled with the parent layout, that is, let the parent layout determine the width of the current control), Wrap_content (exactly what is inside), and the specific value (generally in DP units).
Layout_width: Control height, can be set to Match_parent, Wrap_content, concrete value.
Visibility: visible or not, there are three optional values: visible (visible, do not set this property as default), invisible (transparent, still occupy space on the screen), gone (not visible, not occupy space).
1.TextView (text)

TextView can be said to be the simplest of controls.

1.1 Basic Properties

<!--res/layout/activity_main.xml-->
<linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical" >
 
  <textview
    android:id= "@+id/text" android:layout_width= "Match_parent"
    android:layout_height= "wrap_content"
    android:gravity= "center"
    android:textsize= "30SP"
    Android:textcolor= "#334433"
    android:text= "@string/app_name"
    />
 
</LinearLayout>

Text : The Text property is displayed, @string/app_name represents app_name in the resource file Res/values/strings.xml, or it can be written directly.

<!--res/values/strings.xml-->
<resources>
  <string name= "App_name" >uiexample</ string>
  <string name= "Title_activity_main" >MainActivity</string>
</resources>

Gravity: Set the content position in the TextView, the optional value is top, bottom, left, right, center, etc. You can have more than one value, such as the desired text in the lower-right corner of the TextView, set to Gravity= "Right|bottom", with | Separated
Textsize with textcolor text size and color.
ID: Here is "\@+id/text", which means assigning a unique identifier text to the ID, similar to the reference, with A +.
1.2 Definition Style

If the above style is the style of the title, and is reused many times. If each heading is defined like this, it will not only increase the amount of work but also make it difficult to use the modification, in which case the style abstraction will solve the problem.
This is the same as the role of CSS in web development.

<!--Res/values/styles.xml added texttitle-->
<resources>
  ...
  <style name= "Texttitle" >
    <item name= "Android:textcolor" > #334433 </item>
    <item name= " Android:textsize ">30sp</item>
    <item name=" android:gravity ">center</item>
  </ style>
</resources>
 
<!--res/layout/activity_main.xml TextView can be modified as follows-->
  < TextView
    android:id= "@+id/text"
    android:layout_width= match_parent "android:layout_height=" Wrap
    _content "
    android:text=" @string/app_name "
    style=" @style/texttitle "
    />

1.3 Dynamic operation

Use code to dynamically set text content in TextView public
class Mainactivity extends activity {
  @Override
  protected void OnCreate ( Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    The TextView instance is obtained by Findviewbyid.
    //Use SetText () and GetText () to assign and take values.
    TextView TextView = (TextView) Findviewbyid (r.id.text);
    Textview.settext ("Hello world!");
    LOG.D ("Mainactivity", Textview.gettext (). toString ());

2.ImageView (picture)

There is text, the nature of the picture.

<imageview
  android:id= "@+id/image"
  android:layout_width= "wrap_content"
  android:layout_height = "Wrap_content"
  android:src= "@drawable/test_image"
  />

ID: The id attribute can be omitted if the operation is not dynamic in Java code.
src: that is, the definition of the displayed picture, the need to load the picture placed in the res/drawable/directory.

Call the Setimageresource () method.
//The picture to be loaded is copied to the res/drawable/directory.
ImageView ImageView = (imageview) Findviewbyid (r.id.image);
Imageview.setimageresource (R.drawable.test_image2);

3.Button (Button)

3.1 Basic styles

<button
  android:id= "@+id/button_1"
  android:layout_width= "Match_parent"
  Wrap_content "
  android:text=" I Am A button "
  />"

Text:text is the hint on the button
3.2 Click events

3.2.1 Registers the listener for the Click event

Line 12th, register listener onclicklistener, replication onclick () function. Public
class Mainactivity extends activity {
 
  private TextView TextView;
   
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    TextView = (TextView) Findviewbyid (r.id.text);
    Button button = (button) Findviewbyid (r.id.button_1);
    Button.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (View v) {
        //... Omit Click event
        //For example: Textview.settext ("Hello World");}}

3.2.2 Implementation Interface Onclicklistener

Line 14th, to bind the click event to this
//line 17th, the Onclicklistener () method of the Replication interface ()
//Java can inherit only one class, and the interface can be considered as Java multiple inheritance mode public
Class Mainactivity extends activity implements view.onclicklistener{
 
  private TextView TextView;
   
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    TextView = (TextView) Findviewbyid (r.id.text);
    Button button = (button) Findviewbyid (r.id.button_1);
    Button.setonclicklistener (this);
  }
   
  @Override public
  void OnClick (view view) {
    //based on ID, if there are multiple controls in an activity that need to be tied to a hit event
    ///That way, this method is straightforward and intuitive
    switch ( View.getid ()) {case
      r.id.button_1:
        //... Omit Click event
        //For example: Textview.settext ("Hello world!");
        break;
      Default: Break;}}}

4.EditText (input box)

4.1 Basic Styles

<edittext
  android:id= "@+id/edit"
  android:layout_width= "Match_parent"
  Wrap_content "
  android:maxlines=" 3 "
  android:hint=" please Input ... "
  />

Maxlines: Specifies that the maximum line number for the input box is 3 lines, and after more than 3 lines, the text will scroll up and EditText will not continue to be stretched. If not specified, the EditText will be stretched as the input content increases.
Hint: Similar to HTML placeholder, for input box prompts.
4.2 Getting input

Click on the button to print what you have entered. Public
class Mainactivity extends activity {
   
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    // ...
    Button button = (button) Findviewbyid (r.id.button_1);
    Final EditText EditText = (edittext) Findviewbyid (r.id.edit);
    Button.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (View v) {
        //Use The GetText () method gets the contents of the EditText
        String input_text = Edittext.gettext (). toString ();
        LOG.D ("Mainactivity", Input_text);}}
    );
  }

5.ProgressBar (progress bar)

5.1 Circular progress bar

<progressbar
  android:id= "@+id/progress_bar"
  android:layout_width= "Match_parent"
  android: layout_height= "Wrap_content"
  android:visibility= "Gone"
  />

Visibility: visible (visible), invisible (transparent, occupy space), gone (not visible, not occupy space), the initial value is set to invisible.

Generally more time-consuming work, will temporarily show the progress bar, after the work is completed, the progress bar disappears
//The following code simulates the process public
class Mainactivity extends activities implements view.onclicklistener{
 
  private ProgressBar ProgressBar;
 
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    //...
    Button button = (button) Findviewbyid (r.id.button_1);
    ProgressBar = (ProgressBar) Findviewbyid (R.id.progress_bar);
    Button.setonclicklistener (this);
  }
 
  @Override public
  void OnClick (view view) {
    switch (View.getid ()) {case
      r.id.button_1:
        //Click the button, If the current state is visible, it becomes invisible
        //If it is not visible, it becomes visible if
        (progressbar.getvisibility () = View.gone) {
          Progressbar.setvisibility (view.visible);
        } else {
          progressbar.setvisibility (view.gone);
        }
        break;
      Default:break
    }}
  }

5.2 Horizontal progress bar

<progressbar
  android:id= "@+id/progress_bar"
  android:layout_width= "match_parent"
  android:layout_ height= "Wrap_content"
  android:max= "style=" "
  @style/base.widget.appcompat.progressbar.horizontal"
  />

Style: Set style to horizontal, and other styles can be tried on your own.

Change the OnClick () to
@Override public
void OnClick (view view) {
  switch (View.getid ())
    {case R.id.button _1:
      //Get current progress value, each Click Progress value +10
      int progress = progressbar.getprogress ();
      Progressbar.setprogress (progress +);
    Default:break
  }
}

6.SeekBar (sliding bar)

6.1 Basic Styles

<seekbar
  android:id= "@+id/seek_bar"
  android:layout_width= "Match_parent"
  Wrap_content "
  android:max="
  android:progress= "
  />
<textview android:id=
  " @+id/ Text "
  android:layout_width=" match_parent "
  android:layout_height=" wrap_content "
  android:gravity=" Center "
  />

Max: The maximum slider bar, set to 100
Progress: Initializes the value of the slider bar, set to 50
6.2 Registering the Slide listener

public class Mainactivity extends activity {private TextView TextView;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    TextView = (TextView) Findviewbyid (R.id.text);
    SeekBar SeekBar = (SeekBar) Findviewbyid (R.id.seek_bar);
      Set the slider listener to duplicate three event functions, which are value changes, start sliding, and end sliding Seekbar.setonseekbarchangelistener (new Seekbar.onseekbarchangelistener () { @Override public void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) {if (fro
        Muser) {//the user actively slides, the value changes when triggering the event Textview.settext ("User is sliding, current value:" + progress); @Override public void Onstarttrackingtouch (SeekBar SeekBar) {log.d ("mainactivity", "touch when starting sliding")
      Issue ");
      @Override public void Onstoptrackingtouch (SeekBar SeekBar) {log.d ("mainactivity", "triggering the event at the end of sliding");
  }
    });
 }
}

7.SrollView (scrollable view)

<scrollview
  android:layout_width= "wrap_content"
  android:layout_height= "wrap_content" >
  <!- - ... Omit other controls-->
  <textview
    android:id= "@+id/text"
    android:layout_width= "Match_parent"
    android: layout_height= "Wrap_content"
    />
</ScrollView>

When the TextView content is too much (one page is not complete), then use ScrollView can make the page can be vertical scrolling mode, scroll to see all the content vertically.
8.WebView (browser)

<webview
  android:id= "@+id/web_view"
  android:layout_width= "match_parent"
  android:layout_ height= "Match_parent"
  />

To add access to a network in Androidmanifest.xml

<manifest ... >
  <application ...> ...
  </application>
  <uses-permission android:name= "Android.permission.INTERNET"/>
</ Manifest>
public class Mainactivity extends activity {
 
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    WebView WebView = (webview) Findviewbyid (R.id.web_view);
    Allows the execution of Javasript
    webview.getsettings (). Setjavascriptenabled (true);
    Set proxy, replication shouldoverrideurlloading function
    webview.setwebviewclient (new Webviewclient () {
      @Override
      public boolean shouldoverrideurlloading (webview view,string url) {
        view.loadurl (URL);//Load page return
        true;// True to open a Web page using the current WebView, without using the System browser
      }
    };
    Webview.loadurl ("Http://www.jb51.net");
  }

Run the program, similar to a browser that has a Web page open, just a missing URL entry box.

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.