android笔记:点击按钮显示文本
点击 7603 创建时间 2012-10-30 21:38:28
<p> <strong>activity_main.xml</strong></p> <div class="code"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> xmlns:tools="http://schemas.android.com/tools"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent" ><br /> <br /> <TextView<br /> android:id="@+id/msg"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_alignParentLeft="true"<br /> android:layout_alignParentTop="true"<br /> android:layout_marginLeft="80dp"<br /> android:layout_marginTop="128dp"<br /> android:text="@string/hello_world"<br /> tools:context=".MainActivity" /><br /> <br /> <Button<br /> android:id="@+id/button2"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_below="@+id/msg"<br /> android:layout_marginTop="89dp"<br /> android:onClick="rongFramework"<br /> android:text="@string/buttonRongFramework" /><br /> <br /> <Button<br /> android:id="@+id/button1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_alignLeft="@+id/msg"<br /> android:layout_below="@+id/msg"<br /> android:layout_marginTop="26dp"<br /> android:onClick="showName"<br /> android:text="@string/buttonShowName" /><br /> <br /> </RelativeLayout><br /> <br /> <br /> </div> <p> <strong>MainActivity.java </strong></p> <div class="code"> package com.example.test;<br /> <br /> import android.os.Bundle;<br /> import android.app.Activity;<br /> import android.view.Menu;<br /> import android.view.View;<br /> import android.widget.TextView;<br /> <br /> public class MainActivity extends Activity {<br /> <br /> private TextView msg;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.activity_main);<br /> }<br /> <br /> @Override<br /> public boolean onCreateOptionsMenu(Menu menu) {<br /> getMenuInflater().inflate(R.menu.activity_main, menu);<br /> return true;<br /> }<br /> /* Called when the user touches the button /<br /> public void showName(View view) {<br /> // Do something in response to button click<br /> msg = (TextView) this.findViewById(R.id.msg);<br /> msg.setText("欢迎访问wudimei.com");<br /> <br /> }<br /> <br /> public void rongFramework(View view )<br /> {<br /> msg = (TextView) this.findViewById(R.id.msg);<br /> msg.setText("rong framework是一个php开源框架。");<br /> }<br /> }<br /> </div>
上一篇: 把windows7家庭普通版的应用程序放到D盘
下一篇: gnustep写一个类