Button的onClickListener常用方法之android:onclick属性


   1:  package com.yang.huang;
   2:   
   3:  import android.app.Activity;
   4:  import android.os.Bundle;
   5:  import android.view.View;
   6:  import android.widget.Button;
   7:  import android.widget.TextView;
   8:   
   9:  public class TestonlistenerActivity extends Activity {
  10:      TextView tv;
  11:      Button button;
  12:      /** Called when the activity is first created. */
  13:      @Override
  14:      public void onCreate(Bundle savedInstanceState) {
  15:          super.onCreate(savedInstanceState);
  16:          setContentView(R.layout.main);
  17:          
  18:          button=(Button)findViewById(R.id.button1);
  19:          tv = (TextView)findViewById(R.id.textView1);
  20:         
  21:          }
  22:      
  23:      public void fun_click(View v) {
  24:          tv.setText("单击改变textview的内容");
  25:          
  26:      }
  27:     
  28:      
  29:  }

 

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:      android:layout_width="fill_parent"
   4:      android:layout_height="fill_parent"
   5:      android:orientation="vertical" >
   6:   
   7:      <Button
   8:          android:id="@+id/button1"
   9:          android:layout_width="fill_parent"
  10:          android:layout_height="wrap_content"
  11:          android:onClick="fun_click"
  12:          android:text="@string/hello" />
  13:   
  14:   
  15:   
  16:      <TextView
  17:          android:id="@+id/textView1"
  18:          android:layout_width="173dp"
  19:          android:layout_height="50dp"
  20:          android:text="单击改变"
  21:          android:gravity="center" />
  22:   
  23:  </LinearLayout>

 

这是1.6以后的新方法,在main.xml里给Button添加android:onclick="fun_click";  fun_click就是方法名

然后在类里添加一个fun_click()方法就可以,里面写的是点击按钮实现的动作。这是最简单的方法。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM