Read "Android from entry to entry" (6) -- button, android from entry to entry

Source: Internet
Author: User

Read "Android from entry to entry" (6) -- button, android from entry to entry
Button)

A Button belongs to the android. widget package and inherits android. widget. TextView. It is also the parent class of CompoundButton, CheckBox, RadioButton, and ToggleButton.

Button Method

Button Layout

Supplement later

Button example

Complete: http://download.csdn.net/detail/sweetloveft/9105219
The following program first creates an interface layout and related controls through layout/activity_layout.xml, creates a color table through values/color. xml, and imports necessary resources in onCreate. The function of the program is:
1. Click different buttons to change the background color of textView through the loaded values/color. xml;
2. Press different buttons to display the press status and release status, and change the background color of textView when releasing.
Key requirements:
1. Understand why the modifier final is added before the objects btn1, btn2, and btn3;
2. Learn to add color. xml under the values directory and use Resource to load and import;
3. Learn to use the Color class.

1. MainActivity. java
Package com. sweetlover. buttondemo; import android. app. activity; import android. content. res. resources; import android. graphics. color; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. keyEvent; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {TextVi Ew textView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_layout); Resources res = getBaseContext (). getResources (); textView = (TextView) findViewById (R. id. textView1); // note: the constant object final Button btn1 = (Button) findViewById (R. id. button1); final Button btn2 = (Button) findView ById (R. id. button2); final Button btn3 = (Button) findViewById (R. id. button3); final Drawable drawable_red = res. getDrawable (R. color. RED); final Drawable drawable_green = res. getDrawable (R. color. GREEN); final Drawable drawable_blue = res. getDrawable (R. color. BLUE); btn1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString text = "you clicked" + Btn1.getText (). toString (); textView. setText (text); if (textView. getBackground ()! = Drawable_red) textView. setBackgroundResource (R. color. RED) ;}}); btn2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString text = "you clicked" + btn2.getText (). toString (); textView. setText (text); if (textView. getBackground ()! = Drawable_green) textView. setBackgroundResource (R. color. GREEN) ;}}); btn3.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString text = "you clicked" + btn3.getText (). toString (); textView. setText (text); if (textView. getBackground ()! = Drawable_blue) textView. setBackgroundResource (R. color. BLUE) ;}}) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stubString text = "you press the key! "; TextView. setText (text); return super. onKeyDown (keyCode, event) ;}@ Overridepublic boolean onKeyUp (int keyCode, KeyEvent event) {// TODO Auto-generated method stubString text = "you have released the button! "; TextView. setText (text); textView. setBackgroundColor (Color. WHITE); return super. onKeyUp (keyCode, event );}}

 

2. activity_layout.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentTop = "true" android: layout_centerHorizontal = "true" android: layout_marginTop = "95dp" Ndroid: text = "nothing ...... "Android: background =" @ color/WHITE "/> <Button android: id =" @ + id/button2 "android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" android: text = "Button 2"/> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerVertical = "true" android: layout_marginRight = "14dp" android: layout_toLeftOf = "@ + id/button2" android: text = "Button 1"/> <Button android: id = "@ + id/button3" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: plugin = "true" android: layout_marginLeft = "14dp" android: layout_toRightOf = "@ + id/button2" android: text = "button 3"/> </RelativeLayout>

 

3. color. xml
<?xml version="1.0" encoding="utf-8"?><resources>    <!-- ARGB -->    <color name="BLACK">#FF000000</color>    <color name="DKGRAY">#FF444444</color>    <color name="GRAY">#FF888888</color>    <color name="LTGRAY">#FFCCCCCC</color>    <color name="WHITE">#FFFFFFFF</color>    <color name="RED">#FFFF0000</color>    <color name="GREEN">#FF00FF00</color>    <color name="BLUE">#FF0000FF</color>    <color name="YELLOW">#FFFFFF00</color>    <color name="CYAN">#FF00FFFF</color>    <color name="MAGENTA">#FFFF00FF</color>    <color name="TRANSPARENT">#00000000</color></resources>

 

4. AndroidManifest. xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sweetlover.buttondemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name="com.sweetlover.buttondemo.MainActivity">            <intent-filter>                <category android:name="android.intent.category.LAUNCHER"/>                <action android:name="android.intent.action.MAIN"/>            </intent-filter>        </activity>    </application></manifest>

 

Copyright Disclaimer: This article is an original article by the blogger. For more information, see the source. Thank you for your progress.

Related Article

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.