侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

浅入浅出Android(002):使用Button类做个按钮

2022-07-03 星期日 / 0 评论 / 0 点赞 / 90 阅读 / 5598 字

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>



添加一个按钮后, /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" />    <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>



新建的按钮,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中添加按钮:
<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" />



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



运行结果如下:

广告 广告

评论区