android组件:Button

实现的主要功能是当button按下的时候,将开始设置的TextView中的文本换成自己设定的文本,我这里是将文本设置成click。

先上效果:点击之前

 

button点击之后的效果:

 

 

layout中的xml文件:

layout中是设置了一个文本框和三个按钮

 
  
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     tools:context=".MainActivity" > 
  6.  
  7.     <TextView 
  8.         android:id="@+id/myText"  //给TextView取个名字
  9.         android:layout_width="wrap_content" //设置TextView的宽度为字体包裹
  10.         android:layout_height="wrap_content" 
  11.         android:text="@string/hello_world"   //设置文本框中的内容是从strings.xml中读取的名为hello_world的文本
  12.         android:background="#00ff00" //文本框背景的颜色
  13.         android:textColor="#ff00ff" //设置字体颜色
  14.         /> 
  15.  //下面是添加Button,基本设置和TextView相同
  16.      <Button 
  17.          android:id="@+id/myButton3" 
  18.          android:layout_width="wrap_content" 
  19.          android:layout_height="wrap_content" 
  20.          android:layout_alignParentRight="true" 
  21.          android:layout_below="@+id/myText" 
  22.          android:layout_marginRight="35dp" 
  23.          android:layout_marginTop="32dp" 
  24.          android:text="click3" /> 
  25.  
  26.      <Button 
  27.          android:id="@+id/myButton2" 
  28.          android:layout_width="wrap_content" 
  29.          android:layout_height="wrap_content" 
  30.          android:layout_alignBaseline="@+id/myButton3" 
  31.          android:layout_alignBottom="@+id/myButton3" 
  32.          android:layout_marginRight="38dp" 
  33.          android:layout_toLeftOf="@+id/myButton3" 
  34.          android:text="click2" /> 
  35.  
  36.      <Button 
  37.          android:id="@+id/myButton" 
  38.          android:layout_width="wrap_content" 
  39.          android:layout_height="wrap_content" 
  40.          android:layout_alignBaseline="@+id/myButton2" 
  41.          android:layout_alignBottom="@+id/myButton2" 
  42.          android:layout_alignParentLeft="true" 
  43.          android:layout_marginLeft="16dp" 
  44.          android:text="click1" /> 
  45.           
  46. </RelativeLayout> 

ButtonActivaty.java 文件:

 

 
  
  1. package com.example.buttonproject;    
  2. import android.os.Bundle;    
  3. import android.app.Activity;   
  4. import android.text.method.ScrollingMovementMethod;    
  5. import android.view.View;   
  6. import android.widget.Button;   
  7. import android.widget.TextView;   
  8. import android.view.View.OnClickListener;     
  9. public class MainActivity extends Activity {        
  10.     private Button bt1;        
  11.     private Button bt2;       
  12.     private Button bt3;        
  13.     private TextView tV;                    
  14. @Override        
  15. protected void onCreate(Bundle savedInstanceState) {            
  16.     super.onCreate(savedInstanceState);            
  17.     super.setContentView(R.layout.activity_main);                   
  18.      //通过id号找到我们在layout布局中的main.xml文件中设置的一个TextView和三个Button            
  19.     tV = (TextView)findViewById(R.id.myText);            
  20.     bt1 = (Button)findViewById(R.id.myButton);            
  21.     bt2 = (Button)findViewById(R.id.myButton2);            
  22.     bt3 = (Button)findViewById(R.id.myButton3);             
  23.     //设置click事件监听,当有click事件发生时,将标题栏和我们设置的TextView的          
  24.     //文本设置成被按下的button的内容                 
  25.     OnClickListener l = new OnClickListener() {                              
  26.     @Override                
  27.     public void onClick(View v) {                   
  28.              // 设置标题栏                    
  29.             setTitle("Result:"+((TextView)v).getText());                    
  30.             tV.setText("Result:"+((TextView)v).getText());                                  
  31.         }            
  32.     };           
  33.      //将三个button都绑定上刚刚设置的监听事件          
  34.     bt1.setOnClickListener(l);            
  35.     bt2.setOnClickListener(l);            
  36.     bt3.setOnClickListener(l);            
  37.     tV.setMovementMethod(ScrollingMovementMethod.getInstance());                 
  38.     }      
  39.  }