Android Program Development: (10) basic controls-10.2 Button, ImageButton, EditText, Chc

Source: Internet
Author: User

In addition to the most commonly used TextView, Android also provides some other basic controls.

Button
ImageButton
EditText
CheckBox
RadioGroup and RadioButton
ToggleButton
The following example shows how to use these basic controls.

1. Create a project: BasicViews.

2. Code in main. xml.

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<Button android: id = "@ + id/btnSave"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/save"
Android: onClick = "btnSaved_clicked"/>
 
<Button android: id = "@ + id/btnOpen"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Open"/>
 
<ImageButton android: id = "@ + id/btnImg1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/ic_launcher"/>
 
<EditText android: id = "@ + id/txtName"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
 
<CheckBox android: id = "@ + id/chkAutosave"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Autosave"/>
 
<CheckBox android: id = "@ + id/star"
Style = "? Android: attr/starStyle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
 
 
<RadioGroup android: id = "@ + id/rdbGp1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">

<RadioButton android: id = "@ + id/rdb1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Option 1"/>

<RadioButton android: id = "@ + id/rdb2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Option 2"/>
 
</RadioGroup>
 

<ToggleButton android: id = "@ + id/toggle1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
3. F11 debugging.


4. Click different controls to observe different effects.

 

5. process the event.

[Java]
Package net. learn2develop. BasicViews1;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. CheckBox;
Import android. widget. RadioButton;
Import android. widget. RadioGroup;
Import android. widget. RadioGroup. OnCheckedChangeListener;
Import android. widget. Toast;
Import android. widget. ToggleButton;
 
Public class BasicViews1Activity extends Activity {

Public void btnSaved_clicked (View view ){
DisplayToast ("You have clicked the Save button1 ");
}

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 

// --- Button view ---
Button btnOpen = (Button) findViewById (R. id. btnOpen );
BtnOpen. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
DisplayToast ("You have clicked the Open button ");
}
});

/*
// --- Button view ---
Button btnSave = (Button) findViewById (R. id. btnSave );
BtnSave. setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v ){
DisplayToast ("You have clicked the Save button ");
}
});
*/
 
// --- CheckBox ---
CheckBox checkBox = (CheckBox) findViewById (R. id. chkAutosave );
CheckBox. setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v ){
If (CheckBox) v). isChecked ())
DisplayToast ("CheckBox is checked ");
Else
DisplayToast ("CheckBox is unchecked ");
}
});
 
// --- RadioButton ---
RadioGroup radioGroup = (RadioGroup) findViewById (R. id. rdbGp1 );
RadioGroup. setOnCheckedChangeListener (new OnCheckedChangeListener ()
{
Public void onCheckedChanged (RadioGroup group, int checkedId ){
RadioButton P4 = (RadioButton) findViewById (R. id. rdb1 );
If (rb1.isChecked ()){
DisplayToast ("Option 1 checked! ");
} Else {
DisplayToast ("Option 2 checked! ");
}
}
});
 
// --- ToggleButton ---
ToggleButton toggleButton =
(ToggleButton) findViewById (R. id. toggle1 );
ToggleButton. setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v ){
If (ToggleButton) v). isChecked ())
DisplayToast ("Toggle button is On ");
Else
DisplayToast ("Toggle button is Off ");
}
});
}
 
Private void DisplayToast (String msg)
{
Toast. makeText (getBaseContext (), msg,
Toast. LENGTH_SHORT). show ();
}
 
}

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.