1、“制作”一个按钮 首先在eclipse建立android项目,应用名设为MyButton,其他创建参数取默认值。
默认的项目运行后是一个Hello wold窗口,/res/layout/activity_main.xml默认内容如下:
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
添加一个按钮后,
/res/layout/activity_main.xml内容如下:
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" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
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" />
新建的按钮,id为button1,其内文本内容是“Button”。
运行项目,效果如下:
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;
}
}
首先导入View和Button:
import android.view.View;
import android.widget.Button;
在onCreate方法中添加这些内容:
final Button btn1 = (Button) this.findViewById(R.id.button1);
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
btn1.setText("aaa");
}
});
现在,
/src/
com.example.mybutton/MainActivity.java内容如下:
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;
}
}
运行程序,当点击按钮Button后,其文本内容变成“aaa”。
3、再添加一个按钮用于退出程序 关于退出,具体可见http://www.cnblogs.com/homg/p/3346757.html和http://www.yoyong.com/archives/199,这里使用System.exit(0)退出程序。
在
/res/layout/activity_main.xml中添加按钮:
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" />
在/src/com.example.mybutton/MainActivity.java的onCreate方法中添加以下代码: final Button btn2 = (Button) this.findViewById(R.id.button2);
btn2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
System.exit(0);
}
});
运行结果如下: