Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209767
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2013-12-14 21:46:11

01_06_Android当中的常见控件
常见控件(一)
TextView的使用方法
EditText的使用方法
Button的使用方法
Menu的使用方法

创建MyActivity03工程
Activity03.java

点击(此处)折叠或打开

  1. //Activity03.java
  2. package com.lwb.myactivity03;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13. //1、Activity中要生成四个控件
  14. //2、要为其中的两个空间设置显示的值
  15. //3、创建一个监听器,监听按钮按下的动作
  16. //4、将监听器的对象,绑在按钮对象上
  17. public class Activity03 extends Activity {

  18.         //当客户点击MENU按钮是,调用该方法。Menu菜单的实现,复写一个函数,实际上是一个回调函数,点击屏幕上的Menu就能返回
  19.         @Override
  20.         public boolean onCreateOptionsMenu(Menu menu) {
  21.             // TODO Auto-generated method stub            
  22.             //添加代码如下:
  23.         //    menu.add(groupId, itemId, order, title)
  24.             menu.add(0, 1, 1, R.string.exit);
  25.             menu.add(0,2,2,R.string.about);
  26.             return super.onCreateOptionsMenu(menu);
  27.         }
  28.         
  29.         //当客户点击也是一个回调函数,把点击的对象的item 传入来
  30.         @Override
  31.         public boolean onOptionsItemSelected(MenuItem item) {
  32.             // TODO Auto-generated method stub
  33.             
  34.             if(item.getItemId()==1){//点击的是 退出按钮exit
  35.                 finish(); //退出,即 完成
  36.             }
  37.             
  38.             return super.onOptionsItemSelected(item);
  39.         }    
  40.         



  41.     private EditText factorOne;
  42.     private EditText factorTwo;
  43.     private TextView symbol;
  44.     private Button calculate;
  45.     @Override
  46.     protected void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.         setContentView(R.layout.main);
  49.         
  50.         //根据控件的ID来取得代表控件的对象
  51.         factorOne =(EditText)findViewById(R.id.factorOne);
  52.         factorTwo =(EditText)findViewById(R.id.factorTwo);
  53.         symbol =(TextView)findViewById(R.id.symbol);
  54.         calculate =(Button) findViewById(R.id.calculate);
  55.         
  56.         //为symbol和calculate设置显示的值
  57.         //symbol.setText("乘以");
  58.         //calculate.setText("计算");
  59.         symbol.setText(R.string.symbol);
  60.         calculate.setText(R.string.calculate);
  61.         
  62.         //将监听器对象绑定到按钮对象上面
  63.         calculate.setOnClickListener(new CalculateListener());
  64.     }
  65.     
  66.     
  67.     
  68.     
  69.     class CalculateListener implements OnClickListener{

  70.         @Override
  71.         public void onClick(View arg0) {
  72.             // TODO Auto-generated method stub
  73.             
  74.             //1、取得两个EditText的值            
  75.             String factorOneStr=factorOne.getText().toString();
  76.             String factorTwoStr=factorTwo.getText().toString();
  77.             //2、将两个值存放到Intent对象中
  78.             Intent intent =new Intent();
  79.             intent.putExtra("one", factorOneStr);
  80.             intent.putExtra("Two", factorTwoStr);
  81.             intent.setClass(Activity03.this,ResultActivity.class);
  82.             //3、使用这个Intent启动ResultActivity
  83.             Activity03.this.startActivity(intent);            
  84.         }        
  85.     }    
  86.     /*@Override
  87.     public boolean onCreateOptionsMenu(Menu menu) {
  88.         // Inflate the menu; this adds items to the action bar if it is present.
  89.         getMenuInflater().inflate(R.menu.activity03, menu);
  90.         return true;
  91.     }*/
  92. }
Activity03对应的布局xml文件

点击(此处)折叠或打开

  1. <!--main.xml -->
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText
  8.     android:id="@+id/factorOne"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     />
  12. <TextView
  13.     android:id="@+id/symbol"
  14.     android:layout_width="fill_parent"
  15.     android:layout_height="wrap_content"
  16.    
  17.     />
  18. <EditText
  19.     android:id="@+id/factorTwo"
  20.     android:layout_width="fill_parent"
  21.     android:layout_height="wrap_content"
  22.     />
  23. <Button
  24.     android:id="@+id/calculate"
  25.     android:layout_width="fill_parent"
  26.     android:layout_height="wrap_content"
  27.     />
  28. </LinearLayout>
  29. <!--
  30. <RelativeLayout xmlns:android=""
  31.     xmlns:tools=""
  32.     android:layout_width="match_parent"
  33.     android:layout_height="match_parent"
  34.     android:paddingBottom="@dimen/activity_vertical_margin"
  35.     android:paddingLeft="@dimen/activity_horizontal_margin"
  36.     android:paddingRight="@dimen/activity_horizontal_margin"
  37.     android:paddingTop="@dimen/activity_vertical_margin"
  38.     tools:context=".Activity03" >
  39. <TextView
  40.         android:id="@+id/symbol"
  41.         android:layout_width="wrap_content"
  42.         android:layout_height="wrap_content"
  43.         android:layout_alignLeft="@+id/factorTwo"
  44.         android:layout_below="@+id/factorOne"
  45.         />

  46.     <EditText
  47.         android:id="@+id/factorOne"
  48.         android:layout_width="fill_parent"
  49.         android:layout_height="wrap_content"
  50.         android:layout_alignParentTop="true"
  51.         android:layout_centerHorizontal="true"
  52.         android:layout_marginTop="17dp"
  53.         android:ems="10" />

  54.     <EditText
  55.         android:id="@+id/factorTwo"
  56.         android:layout_width="fill_parent"
  57.         android:layout_height="wrap_content"
  58.         android:layout_alignLeft="@+id/calculate"
  59.         android:layout_below="@+id/calculate"
  60.         android:layout_marginTop="14dp"
  61.         android:ems="10" />

  62.     <Button
  63.         android:id="@+id/calculate"
  64.         android:layout_width="fill_parent"
  65.         android:layout_height="wrap_content" />
  66.         </RelativeLayout>
  67.  -->

ResultActivity.java

点击(此处)折叠或打开

  1. //ResultActivity.java
  2. package com.lwb.myactivity03;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7. //1、首先接受从Activity03当中所传递的值
  8. //2、计算两个值的积
  9. //3、将计算的结果显示到Activity上
  10. public class ResultActivity extends Activity {
  11.     
  12.     private TextView resultView=null;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         // TODO Auto-generated method stub
  16.         super.onCreate(savedInstanceState);
  17.         
  18.         setContentView(R.layout.result);
  19.         resultView=(TextView)findViewById(R.id.result);
  20.         //得到Intent对象的值
  21.         Intent intent= getIntent();
  22.         String factorOneStr=intent.getStringExtra("one");
  23.         String factorTwoStr=intent.getStringExtra("Two");
  24.         int factorOneInt=Integer.parseInt(factorOneStr);
  25.         int factorTwoInt=Integer.parseInt(factorTwoStr);
  26.         //计算乘积
  27.         int result=factorOneInt*factorTwoInt;
  28.         resultView.setText(result+"");//整型+字符串 强制为 整型
  29.     }    
  30. }
ResultActivity它对应的布局xml文件

点击(此处)折叠或打开

  1. <!-- result.xml -->
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <TextView
  8.      android:id="@+id/result"
  9.      android:layout_width="fill_parent"
  10.      android:layout_height="wrap_content"
  11.      />    
  12. </LinearLayout>
  13. <!--
  14. <?xml version="1.0" encoding="utf-8"?>
  15. <LinearLayout xmlns:android=""
  16.     android:orientation="vertical"
  17.     android:layout_with="fill_parent"
  18.     android:layout_height="fill_parent"
  19.     >
  20.     
  21. </LinearLayout>
  22. -->

//string.xml

点击(此处)折叠或打开

  1. //string.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <resources>

  4.     <string name="app_name">MyActivity03</string>
  5.     <string name="action_settings">Settings</string>
  6.     <string name="hello_world">Hello world!</string>
  7.     <string name="symbol">乘以</string>
  8.     <string name="calculate">计算</string>
  9.     <string name="resultLable">结果Activity</string>
  10.     
  11.     <string name="exit">退出</string>
  12.     <string name="about">关于</string>
  13. </resources>
Manifest.xml文件

点击(此处)折叠或打开

  1. <!--Manifest.xml-->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <manifest xmlns:android=""
  4.     package="com.lwb.myactivity03"
  5.     android:versionCode="1"
  6.     android:versionName="1.0" >

  7.     <uses-sdk
  8.         android:minSdkVersion="10"
  9.         android:targetSdkVersion="17" />

  10.     <application
  11.         android:allowBackup="true"
  12.         android:icon="@drawable/ic_launcher"
  13.         android:label="@string/app_name"
  14.         android:theme="@style/AppTheme" >
  15.         <activity
  16.             android:name="com.lwb.myactivity03.Activity03"
  17.             android:label="@string/app_name" >
  18.             <intent-filter>
  19.                 <action android:name="android.intent.action.MAIN" />

  20.                 <category android:name="android.intent.category.LAUNCHER" />
  21.             </intent-filter>
  22.         </activity>
  23.         
  24.         <activity
  25.             android:name="com.lwb.myactivity03.ResultActivity"
  26.             android:label="@string/resultLable"/>
  27.          <!-- 上面的android:name="com.lwb.myactivity03.ResultActivity" 是不能写成android:name=".ResultActivity"的否则会报错 -->
  28.          <!-- ResultActivity是第二个Activity的类名 -->
  29.     </application>
  30. </manifest>
R.java文件

点击(此处)折叠或打开

  1. /* AUTO-GENERATED FILE. DO NOT MODIFY.
  2.  *
  3.  * This class was automatically generated by the
  4.  * aapt tool from the resource data it found. It
  5.  * should not be modified by hand.
  6.  */

  7. package com.lwb.myactivity03;

  8. public final class R {
  9.     public static final class attr {
  10.     }
  11.     public static final class dimen {
  12.         /** Default screen margins, per the Android Design guidelines.

  13.          Customize dimensions originally defined in res/values/dimens.xml (such as
  14.          screen margins) for sw720dp devices (e.g. 10
TextView的使用方法
    private TextView symbol;
    symbol    =(TextView)findViewById(R.id.symbol);

    private TextView resultView=null;
    resultView=(TextView)findViewById(R.id.result);
    resultView.setText(result+"");//整型+字符串 强制为 整型
    

EditText的使用方法
    private EditText factorOne;
    private EditText factorTwo;
    //根据控件的ID来取得代表控件的对象
    factorOne =(EditText)findViewById(R.id.factorOne);
    factorTwo =(EditText)findViewById(R.id.factorTwo);    
   

Button的使用方法
     private Button calculate;
    calculate =(Button)  findViewById(R.id.calculate);
    calculate.setText(R.string.calculate);

Menu的使用方法

点击(此处)折叠或打开

  1. //当客户点击MENU按钮是,调用该方法。Menu菜单的实现,复写一个函数,实际上是一个回调函数,点击屏幕上的Menu就能返回
  2.         @Override
  3.         public boolean onCreateOptionsMenu(Menu menu) {
  4.             // TODO Auto-generated method stub            
  5.             //添加代码如下:
  6.         //    menu.add(groupId, itemId, order, title)
  7.             menu.add(0, 1, 1, R.string.exit);
  8.             menu.add(0,2,2,R.string.about);
  9.             return super.onCreateOptionsMenu(menu);
  10.         }        
  11.         //当客户点击也是一个回调函数,把点击的对象的item 传入来
  12.         @Override
  13.         public boolean onOptionsItemSelected(MenuItem item) {
  14.             // TODO Auto-generated method stub
  15.             
  16.             if(item.getItemId()==1){//点击的是 退出按钮exit
  17.                 finish(); //退出,即 完成
  18.             }            
  19.             return super.onOptionsItemSelected(item);
  20.         }

涉及到前一课的知识点(即Intent的使用,以及数据的传递):

点击(此处)折叠或打开

  1. //1、取得两个EditText的值            
  2.             String factorOneStr=factorOne.getText().toString();
  3.             String factorTwoStr=factorTwo.getText().toString();
  4.             //2、将两个值存放到Intent对象中
  5.             Intent intent =new Intent();
  6.             intent.putExtra("one", factorOneStr);
  7.             intent.putExtra("Two", factorTwoStr);
  8.             intent.setClass(Activity03.this,ResultActivity.class);
  9.             //3、使用这个Intent启动ResultActivity
  10.             Activity03.this.startActivity(intent);

  11.         Intent intent= getIntent();
  12.         String factorOneStr=intent.getStringExtra("one");
  13.         String factorTwoStr=intent.getStringExtra("Two");
  14.         int factorOneInt=Integer.parseInt(factorOneStr);
  15.         int factorTwoInt=Integer.parseInt(factorTwoStr);
  16.         //计算乘积
  17.         int result=factorOneInt*factorTwoInt;
  18.         resultView.setText(result+"");


OK,比较简单的啦,哈哈。加油啦







阅读(754) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~