展会信息港展会大全

Android游戏开发系统控件-EditText
来源:互联网   发布日期:2016-01-06 19:16:19   浏览:1944次  

导读:Android游戏开发系统控件-EditTextEditText(输入框)是与用户交互数据常用的组件,例如在登录游戏,输入账号、密码等信息时经常用到。创建项目“EditTextProjec...

Android游戏开发系统控件-EditText

EditText(输入框)是与用户交互数据常用的组件,例如在登录游戏,输入账号、密码等信息时经常用到。

创建项目“EditTextProject”

功能:在输入框中输入内容并能显示到TextView当中

项目运行结果截图:

项目源码如下:

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:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

android:id="@+id/tv"

/>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="提示消息"

android:id="@+id/et"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="获取EditText内容!"

android:id="@+id/btn"

/>

</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:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

android:id="@+id/tv"

/>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="提示消息"

android:id="@+id/et"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="获取EditText内容!"

android:id="@+id/btn"

/>

</LinearLayout>

string.xml

[html]

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World,EditTextProject</string>

<string name="app_name">EditTextProject</string>

</resources>

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World,EditTextProject</string>

<string name="app_name">EditTextProject</string>

</resources>

EditTextProject.java

[java]

package com.editText;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class EditTextProject extends Activity implements OnClickListener{

private EditText et;//创建一个文本编辑的对象

private Button btn;

private TextView tv;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

et = (EditText)findViewById(R.id.et);//实例化文本编辑

btn = (Button)findViewById(R.id.btn);

btn.setOnClickListener(this);//注册监听器

tv = (TextView)findViewById(R.id.tv);

}

public void onClick(View v) {

if(v==btn){

//获取EditText中的文本内容

String str = et.getText().toString();

//让TextView将获取到的EditText内容str显示出来

tv.setText(str);

}

}

}

作者:wwj

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

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