Android游戏开发系统控件-Button

 Android游戏开发系统控件-Button

Button(按钮)是一个常用的系统小组件,很小但是在开发中最常用到。一般通过与监听器使用,从而触发一些特定事件。

下面为一个Andriod项目“ButtonProject”,对应的代码如下

作者:wwj

项目功能:点击按钮触发事件

代码分别为:

main.xml

string.xml

ButtonProject.java

项目运行效果图:

代码清单:

=》》main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button 
        android:id="@+id/btn_ok"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_ok"
        />
    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_cancel"
        />

</LinearLayout>

=》》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("取消按钮触发事件!");
    	}	
	}
}


 

 

posted on 2012-05-10 19:07  1.曲待续  阅读(186)  评论(0编辑  收藏  举报

导航