android学习——按钮事件与编辑框事件触发

 

文本框跟编辑框很类似。所以只对编辑框给出示例。

 

第一步,还是创建android项目。

 

第二步。修改已经生成的res/layout/main.xml。整体替换为:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6. >  
  7.  <!-- 定义第一个button的布局  -->    
  8. <Button  
  9.  android:id="@+id/button"  
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     android:gravity="right"  
  13.     android:text="这是button"/>  
  14.    
  15.  <!-- 定义第二个button的布局 -->   
  16. <Button  
  17.  android:id="@+id/edit_view_button"  
  18.  android:layout_width="wrap_content"  
  19.  android:layout_height="wrap_content"  
  20.  android:text="EditView"/>  
  21.   
  22.  </LinearLayout>  

 

注释已经说明每项的定义。具体参数的含义请参考上一篇:http://blog.csdn.net/applezhengxd/archive/2010/05/06/5562199.aspx

 

第三步,

修改自动生成的java类,替换内容为:

 

  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.util.Log;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8.   
  9. public class TextView extends Activity implements OnClickListener {  
  10.    
  11.       
  12.     //定义button   
  13.     Button button = null;  
  14.     Button edit_view_button = null;  
  15.       
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.        
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.          
  22.         find_and_modif_button();  
  23.    }  
  24.      
  25.     private void find_and_modif_button(){  
  26.         
  27.       //根据ID获得XML定义的Button     
  28.       button = (Button)findViewById(R.id.button);  
  29.       button.setOnClickListener(this);  
  30.           
  31.       edit_view_button =(Button)findViewById(R.id.edit_view_button);  
  32.       edit_view_button.setOnClickListener(this);  
  33.   
  34.     }  
  35.      
  36.   
  37.     @Override  
  38.     public void onClick(View v) {  
  39.         // TODO Auto-generated method stub   
  40.         //事件监听   
  41.         if(v == button){  
  42.             setTitle("我是button");  
  43.         }else if(v == edit_view_button){  
  44.             Intent intent = new Intent();  
  45.             intent.setClass(TextView.this,EditTextActivity.class);  
  46.             startActivity(intent);  
  47.         }else{  
  48.             //其他处理   
  49.         }  
  50.     }}  

 

第四步,

 

新建控制editView的XML:editview.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.   <!-- 定义编辑框的布局 -->  
  8.   <EditText  
  9.   android:id="@+id/edit_text"  
  10.   android:text="请输入内容!"  
  11.  android:layout_width="fill_parent"  
  12.  android:layout_height="wrap_content">  
  13.  </EditText>  
  14.    
  15.  <!-- 定义按钮的布局 -->  
  16.  <Button  
  17.   android:id="@+id/get_edit_view_button"  
  18.   android:layout_width="wrap_content"  
  19.   android:layout_height="wrap_content"  
  20.   android:text="获取EditView的值">  
  21.  </Button>  
  22.   
  23.  </LinearLayout>  

 

第五步,

新建编辑框监听类:EditTextActivity

 

  1. import android.app.Activity;   
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.widget.Button;  
  6. import android.widget.EditText;  
  7.   
  8.   
  9. public class EditTextActivity extends Activity implements OnClickListener{  
  10.       
  11.     EditText edit_text = null;  
  12.     CharSequence edit_text_value = null;  
  13.     Button get_edit_view_button = null;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.   
  17.         super.onCreate(savedInstanceState);  
  18.         setTitle("ViewTextActivity");  
  19.         setContentView(R.layout.editview);  
  20.         //根据ID获得XML定义的editView   
  21.         get_edit_view_button = (Button) findViewById(R.id.get_edit_view_button);  
  22.         get_edit_view_button.setOnClickListener(this);  
  23.         edit_text = (EditText) findViewById(R.id.edit_text);  
  24.         edit_text.setOnClickListener(this);  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onClick(View v) {  
  29.         // TODO Auto-generated method stub   
  30.         if(v == get_edit_view_button){  
  31.             edit_text_value = edit_text.getText();  
  32.             setTitle("EditText的值:" + edit_text_value);  
  33.         }else if(v == edit_text){  
  34.               
  35.             if(edit_text.getText().toString().equals("请输入内容!")){  
  36.                 edit_text.setText("");  
  37.             }  
  38.         }  
  39.           
  40.     }  
  41. }  

 

 

第六步,

也是最重要的一步,因为我们新建了一个Activity,所以要对这个Activity进行加载。在AndroidManifest.xml的

 

application标签内加入

   
        <activity android:name="EditTextActivity">
       
        </activity>

 

这段代码,结果如下:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.testview"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".TextView"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.         <!-- 指定类名用于加载 -->       
  16.         <activity android:name="EditTextActivity">   
  17.           
  18.         </activity>  
  19.     </application>  
  20.   
  21. </manifest>   

 

 

至此。整个例子就可以顺利运行了

 

小结:

 

不难看出,使用xml进行布局方便快捷、思路清晰。这个例子中用到了两个Activity,实现了他们之间的调用关系,

 

尤其需要注意的是在AndroidManifest.xml中添加了新Activity的类名。不然程序会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值