1、“制作”一个按钮 首先在eclipse建立android项目,应用名设为MyButton,其他创建参数取默认值。 默认的项目运行后是一个Hello wold窗口,/res/layout/act
1、“制作”一个按钮
首先在eclipse建立android项目,应用名设为MyButton,其他创建参数取默认值。默认的项目运行后是一个Hello wold窗口,/res/layout/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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="38dp" android:text="Button" /></RelativeLayout>
运行项目,效果如下:

2、给按钮添加一个单击事件
在/src/com.example.mybutton中的MainActivity.java初始内容如下:package com.example.mybutton;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
import android.view.View;import android.widget.Button;
final Button btn1 = (Button) this.findViewById(R.id.button1); btn1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { btn1.setText("aaa"); } });
package com.example.mybutton;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //--add final Button btn1 = (Button) this.findViewById(R.id.button1); btn1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { btn1.setText("aaa"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}

3、再添加一个按钮用于退出程序
关于退出,具体可见http://www.cnblogs.com/homg/p/3346757.html和http://www.yoyong.com/archives/199,这里使用System.exit(0)退出程序。在 /res/layout/activity_main.xml中添加按钮:
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="98dp" android:text="exit" />
final Button btn2 = (Button) this.findViewById(R.id.button2); btn2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { System.exit(0); } });
