=》》string.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, ButtonProject!</string>
- <string name="app_name">ButtonProject</string>
- <string name="btn_ok">确定</string>
- <string name="btn_cancel">取消</string>
- </resources>
=》》ButtonProject.java
- package com.buttonProject;
- import Android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class ButtonProject extends Activity implements OnClickListener{
- private Button btn_ok,btn_cancel; //声明两个按钮对象
- private TextView tv; //声明文本视图对象
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //对btn_ok对象进行实例化
- btn_ok = (Button) findViewById(R.id.btn_ok);
- //对btn_cancel对象进行实例化
- btn_cancel = (Button) findViewById(R.id.btn_cancel);
- //对tv对象进行实例化
- tv =(TextView) findViewById(R.id.tv);
- //将btn_ok按钮绑定在点击监听器上
- btn_ok.setOnClickListener(this);
- //将btn_cancel按钮绑定在点击监听器上
- btn_cancel.setOnClickListener(this);
- }
- //使用点击监听器必须重写其抽象函数,
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v == btn_ok){
- tv.setText("确定按钮触发事件!");
- }else if(v == btn_cancel){
- tv.setText("取消按钮触发事件!");
- }
- }
- }
- package com.buttonProject;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class ButtonProject extends Activity implements OnClickListener{
- private Button btn_ok,btn_cancel; //声明两个按钮对象
- private TextView tv; //声明文本视图对象
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //对btn_ok对象进行实例化
- btn_ok = (Button) findViewById(R.id.btn_ok);
- //对btn_cancel对象进行实例化
- btn_cancel = (Button) findViewById(R.id.btn_cancel);
- //对tv对象进行实例化
- tv =(TextView) findViewById(R.id.tv);
- //将btn_ok按钮绑定在点击监听器上
- btn_ok.setOnClickListener(this);
- //将btn_cancel按钮绑定在点击监听器上
- btn_cancel.setOnClickListener(this);
- }
- //使用点击监听器必须重写其抽象函数,
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v == btn_ok){
- tv.setText("确定按钮触发事件!");
- }else if(v == btn_cancel){
- tv.setText("取消按钮触发事件!");
- }
- }
- }
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
