Android cainiao study note 12 ---- Android controls (1) several common simple controls and common android controls

Source: Internet
Author: User

Android cainiao study note 12 ---- Android controls (1) several common simple controls and common android controls

Refer to the first line of code

1. TextView:

Similar to the Label control in traditional desktop application development, it is used to display text information.

For example:

 1 <TextView 2  3         android:layout_width="wrap_content" 4  5         android:layout_height="wrap_content" 6  7         android:textColor="#0000ff" 8  9         android:textSize="40sp"10 11         android:text="@string/hello_world" />

Display Effect:

 

The preceding xml Code sets several common attributes:

Android: layout_width and android: layout_height respectively set the width and height of the control.

TextColor: Set the color of the displayed text.

TextSize: Set the font size of the displayed text.

Text sets the displayed text content.

2. Button:

We have used a lot of the above. We often use the get button through id and then bind and click the listener event. Here we only list one example:

Activity_main.xml:

 1 <TextView 2  3         android:id="@+id/tv" 4  5         android:layout_width="wrap_content" 6  7         android:layout_height="wrap_content" 8  9         android:textColor="#0000ff"10 11         android:textSize="40sp"12 13         android:text="@string/hello_world" />14 15     <Button16 17         android:id="@+id/btn"18 19         android:layout_below="@id/tv"20 21         android:layout_width="wrap_content"22 23         android:layout_height="wrap_content"24 25         android:text="@string/btnText"/>

MainActivity. java:

 1 public class MainActivity extends ActionBarActivity { 2  3   4  5     private TextView tv; 6  7       private Button btn; 8  9  10 11       @Override12 13     protected void onCreate(Bundle savedInstanceState) {14 15         super.onCreate(savedInstanceState);16 17         setContentView(R.layout.activity_main);18 19        20 21         btn = (Button) findViewById(R.id.btn);22 23         tv = (TextView) findViewById(R.id.tv);24 25         btn.setOnClickListener(new OnClickListener() {26 27                 28 29                  @Override30 31                  public void onClick(View v) {32 33                       // TODO Auto-generated method stub34 35                       tv.setText("It's changed!");36 37                  }38 39            });40 41     }42 43 }

3. EditText:

That is, the Text input box. modify the program as follows. Add an EditText on the button. click the button to get the value of EditText and set it to the Text attribute of TextView:

Activity_main.xml:

 

 1  <TextView 2  3         android:id="@+id/tv" 4  5         android:layout_width="wrap_content" 6  7         android:layout_height="wrap_content" 8  9         android:textColor="#0000ff"10 11         android:textSize="40sp"12 13         android:text="@string/hello_world" />14 15     <EditText16 17         android:id="@+id/et"18 19         android:layout_below="@id/tv"20 21         android:layout_width="wrap_content"22 23         android:layout_height="wrap_content"24 25         android:hint="@string/hintText"26 27         />28 29       <Button30 31         android:id="@+id/btn"32 33         android:layout_below="@id/et"34 35         android:layout_width="wrap_content"36 37         android:layout_height="wrap_content"38 39         android:text="@string/btnText"/>

 

MainActivity. java:

 1 public class MainActivity extends ActionBarActivity { 2  3   4  5     private TextView tv; 6  7       private Button btn; 8  9       private EditText et;10 11  12 13       @Override14 15     protected void onCreate(Bundle savedInstanceState) {16 17         super.onCreate(savedInstanceState);18 19         setContentView(R.layout.activity_main);20 21        22 23         btn = (Button) findViewById(R.id.btn);24 25         tv = (TextView) findViewById(R.id.tv);26 27         et = (EditText)findViewById(R.id.et);28 29         btn.setOnClickListener(new OnClickListener() {30 31                 32 33                  @Override34 35                  public void onClick(View v) {36 37                       // TODO Auto-generated method stub38 39                       Editable text = et.getText();40 41                       tv.setText(text.toString());42 43                  }44 45            });46 47     }48 49 }

 

Running effect:

 

Enter the value and click:

 

Note that because the layout_height attribute of EditText is wrap_content, The layout_height attribute will increase as the input content increases, affecting the overall layout. If you want to fix its height, you can set the maxLines attribute to set the maximum number of lines to be displayed, and other content to scroll up.

For example, android: maxLines = "1"

The height of EditText will not change.

4. ImageView:

A widget used to display images. It was used in previous programs. Of course, its main attribute must be to display the source of the image, that is, the android: src attribute, the image to be displayed is stored in res/drawableand the title is hero.png. To display the image, set android: src = "@ drawable/hero.

1 <ImageView2 3         android:id="@+id/iv"4 5         android:layout_width="wrap_content"6 7         android:layout_height="wrap_content"8 9         android:src="@drawable/hero"/>

Display result:

 

5. ProgressBar:

That is, the progress bar. You can set different display styles using the style attribute:

1) do not set the style attribute or set it to style = "? Android: attr/progressBarStyle ", circular display

 

2) style = "? Android: attr/progressBarStyleHorizontal ", horizontal bar display

 

3) style = "? Android: attr/progressBarStyleLarge ", large ring display

 

4) style = "? Android: attr/progressBarStyleSmall ", small

 

The progress bar is used to display the progress. You can use findViewById () to obtain the ProgressBar, and then use setProgress () to set the current progress. You can use getProgress () to get the current progress.

For example:

Layout code:

 1 <ProgressBar 2  3           android:id="@+id/pb" 4  5           android:layout_width="match_parent" 6  7           android:layout_height="wrap_content" 8  9           style="?android:attr/progressBarStyleHorizontal"10 11           android:max="100"12 13           />14 15       <Button16 17           android:id="@+id/btn"18 19           android:layout_below="@id/pb"20 21           android:layout_width="wrap_content"22 23           android:layout_height="wrap_content"24 25           android:text="@string/add_progress"/>

Activity Code:

 1 public class MainActivity extends ActionBarActivity { 2  3     private ProgressBar pb; 4  5       private Button btn; 6  7       @Override 8  9     protected void onCreate(Bundle savedInstanceState) {10 11         super.onCreate(savedInstanceState);12 13         setContentView(R.layout.activity_main);14 15         pb = (ProgressBar) findViewById(R.id.pb);16 17         btn = (Button) findViewById(R.id.btn);18 19         Log.i("PB",pb.getProgress()+"");20 21         btn.setOnClickListener(new OnClickListener() {22 23                 24 25                  @Override26 27                  public void onClick(View v) {28 29                       // TODO Auto-generated method stub30 31                       Log.i("PB",pb.getProgress()+"");32 33                       pb.setProgress(pb.getProgress()+10);34 35                  }36 37            });38 39     }40 41 }

 

Running result:

 

Initially, the default progress is 0.

After clicking the button multiple times:

 

When the maximum value set for android: max is reached, the addition will not change.

6. AlertDialog:

This control is a pop-up dialog box similar to the modal dialog box in desktop development. You must close this dialog box to perform subsequent interaction and display important content.

The constructor of AlertDialog is protected. You cannot directly create AlertDialog through constructor, but you can create AlertDialog through its internal class Builder.

For more information about the internal class, see help in the help manual. The following is a simple example:

 1 AlertDialog.Builder dialog = new AlertDialog.Builder(this); 2  3         dialog.setTitle("Warning"); 4  5         dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 6  7                  8  9                  @Override10 11                  public void onClick(DialogInterface dialog, int which) {12 13                       // TODO Auto-generated method stub14 15                      16 17                  }18 19            });20 21         dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {22 23                 24 25                  @Override26 27                  public void onClick(DialogInterface dialog, int which) {28 29                       // TODO Auto-generated method stub30 31                      32 33                  }34 35            });36 37         dialog.setMessage("warning, hahaha");38 39         dialog.show();

Running result:

 

7. ProgressDialog:

Similar to AlertDialog, AlertDialog is also a dialog box, but it displays a progress bar, as if it is a combination of the two widgets of the dialog box and progress bar.

1 ProgressDialog pd = new ProgressDialog(this);2 3 pd.setTitle("Data Loading...");4 5 pd.show();

Running result:

 

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.