杨庆荣的博客

工作之余接单:公司产品展示网站建设、电商网站建设、小程序、app开发。+86 13714715608

android笔记:点击按钮显示文本

点击 7603    创建时间 2012-10-30 21:38:28

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 xmlns:tools="http://schemas.android.com/tools"  
 android:layout_width="match_parent"  
 android:layout_height="match_parent" >  

 <TextView  
 android:id="@+id/msg"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_alignParentLeft="true"  
 android:layout_alignParentTop="true"  
 android:layout_marginLeft="80dp"  
 android:layout_marginTop="128dp"  
 android:text="@string/hello_world"  
 tools:context=".MainActivity" />  

 <Button  
 android:id="@+id/button2"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_below="@+id/msg"  
 android:layout_marginTop="89dp"  
 android:onClick="rongFramework"  
 android:text="@string/buttonRongFramework" />  

 <Button  
 android:id="@+id/button1"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_alignLeft="@+id/msg"  
 android:layout_below="@+id/msg"  
 android:layout_marginTop="26dp"  
 android:onClick="showName"  
 android:text="@string/buttonShowName" />  

 </RelativeLayout>  

MainActivity.java

 package com.example.test;  

 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.view.View;  
 import android.widget.TextView;  

 public class MainActivity extends Activity {  

     private TextView msg;  
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.activity_main);  
     }  

     @Override  
     public boolean onCreateOptionsMenu(Menu menu) {  
         getMenuInflater().inflate(R.menu.activity_main, menu);  
         return true;  
     }  
     /** Called when the user touches the button */  
     public void showName(View view) {  
         // Do something in response to button click  
         msg = (TextView) this.findViewById(R.id.msg);  
         msg.setText("欢迎访问wudimei.com");  

     }  

     public void rongFramework(View view )  
     {  
         msg = (TextView) this.findViewById(R.id.msg);  
         msg.setText("rong framework是一个php开源框架。");  
     }  
 }  

上一篇: 把windows7家庭普通版的应用程序放到D盘
下一篇: gnustep写一个类