安卓 java 新建一个按钮_浅入浅出Android(002):使用Button类做个按钮

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”。

运行项目,效果如下:

510496d8ce824680d9794a998fd43d8b.png

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”。

78b5008890ff585f64f47b9f05ec05a8.png

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);

}

});

运行结果如下:

2a9f2de4b6a639c1355923dcd7353010.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值