展会信息港展会大全

Android游戏开发系统控件-Button
来源:互联网   发布日期:2015-12-07 15:38:07   浏览:2092次  

导读:Android游戏开发系统控件-ButtonButton(按钮)是一个常用的系统小组件,很小但是在开发中最常用到。一般通过与监听器使用,从而触发一些特定事件。下面为一个Andriod项目...

Android游戏开发系统控件-Button

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

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

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

代码分别为:

main.xml

string.xml

ButtonProject.java

项目运行效果图:

代码清单:

=》》main.xml

[html]

<?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>

<?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

[html]

<?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>

<?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

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

}

}

}

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

}

}

}

作者:wwj

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港