文本框常用属性
1、text:文本框的文本内容
2、maxLines:最大行数
3、lines:行数
4、inputType:输入数据类型
5、hint:提示
6、textColor:文本颜色
7、textSize:文本字号
8、textColorHint:提示文本颜色
9、singleLine:文本是否单行(true, false)
涉及知识点
1、线性布局(LinearLayout)
2、标签(TextView)
3、编辑框(EditText)
4、按钮(Button)
5、意图(Intent)
6、数据包(Bundle)
实现步骤
1.准备两张背景图片,一个用于注册界面背景(registrationbg),一个用于输出信息界面背景(informationbg),将图片拷贝到mipmap中
2.将默认xml的名字改为activity_registration,将默认java文件的名字改为RegistrationActivity。
3.新建一个Java文件,看图
这样基于模板创建会自动注册窗口,比较方便!
4.我们现在可以在activity_registration编写代码了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/registerbg"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_gender"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_age"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_phone"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_email"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_home_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_page"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_home_page"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textUri"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:id="@+id/tv_memo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/memo"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_memo"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:lines="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btn_register"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:onClick="doRegister"
android:text="@string/register" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:onClick="doCancel"
android:text="@string/cancel" />
</LinearLayout>
</LinearLayout>
5.我们现在可以写information_registration的代码了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/informationbg"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="phone"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="email"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_home_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="web"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_memo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
</LinearLayout>
这个时候information_activity中是没有任何数据的,因为我们要从RegistrationActivity.java的文件传送过去。
6.编写string.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">UserRegistration</string>
<string name="action_settings">设置</string>
<string name="name">姓名:</string>
<string name="gender">性别:</string>
<string name="age">年龄:</string>
<string name="phone">电话:</string>
<string name="email">邮箱:</string>
<string name="home_page">主页:</string>
<string name="memo">备注:</string>
<string name="register">注册</string>
<string name="cancel">取消</string>
</resources>
7.我们现在写很关键的RegistrationActivity.java
package net.hw.userregistration;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class RegistrationActivity extends Activity {
private EditText edtName;
private EditText edtGender;
private EditText edtAge;
private EditText edtPhone;
private EditText edtEmail;
private EditText edtHomePage;
private EditText edtMemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局文件设置用户界面
setContentView(R.layout.activity_registration);
// 通过资源标识获得控件示例
edtName = (EditText) findViewById(R.id.edt_name);
edtGender = (EditText) findViewById(R.id.edt_gender);
edtAge = (EditText) findViewById(R.id.edt_age);
edtPhone = (EditText) findViewById(R.id.edt_phone);
edtEmail = (EditText) findViewById(R.id.edt_email);
edtHomePage = (EditText) findViewById(R.id.edt_home_page);
edtMemo = (EditText) findViewById(R.id.edt_memo);
}
/**
* 注册按钮单击事件处理方法
*
* @param view
*/
public void doRegister(View view) {
// 获取用户输入的数据
String strName = edtName.getText().toString();
String strGender = edtGender.getText().toString();
String strAge = edtAge.getText().toString();
String strPhone = edtPhone.getText().toString();
String strEmail = edtEmail.getText().toString();
String strHomePage = edtHomePage.getText().toString();
String strMemo = edtMemo.getText().toString();
// 将输入的各项数据打包
Bundle bundle = new Bundle();
bundle.putString("name", strName);
bundle.putString("gender", strGender);
bundle.putString("age", strAge);
bundle.putString("phone", strPhone);
bundle.putString("email", strEmail);
bundle.putString("home_page", strHomePage);
bundle.putString("memo", strMemo);
// 创建意图,指明起始窗口与目标窗口
Intent intent = new Intent(RegistrationActivity.this,InformationActivity.class);
// 携带数据包
intent.putExtras(bundle);
// 按意图启动窗口
startActivity(intent);
}
/**
* 取消按钮单击事件处理方法
*
* @param view
*/
public void doCancel(View view) {
finish();
}
}
8.接下来是InformationActivity.java文件
package net.hw.userregistration;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class InformationActivity extends Activity {
private TextView tvName;
private TextView tvGender;
private TextView tvAge;
private TextView tvPhone;
private TextView tvEmail;
private TextView tvHomePage;
private TextView tvMemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局文件设置用户界面
setContentView(R.layout.activity_information);
// 通过资源标识获得控件示例
tvName = (TextView) findViewById(R.id.tv_name);
tvGender = (TextView) findViewById(R.id.tv_gender);
tvAge = (TextView) findViewById(R.id.tv_age);
tvPhone = (TextView) findViewById(R.id.tv_phone);
tvEmail = (TextView) findViewById(R.id.tv_email);
tvHomePage = (TextView) findViewById(R.id.tv_home_page);
tvMemo = (TextView) findViewById(R.id.tv_memo);
// 获得意图
Intent intent = getIntent();
if (intent != null) {
// 获得意图携带的数据包
Bundle bundle = intent.getExtras();
// 从数据包里按键取值
String strName = bundle.getString("name");
String strGender = bundle.getString("gender");
String strAge = bundle.getString("age");
String strPhone = bundle.getString("phone");
String strEmail = bundle.getString("email");
String strHomePage = bundle.getString("home_page");
String strMemo = bundle.getString("memo");
// 设置各个标签的内容
tvName.setText("姓名:" + strName);
tvGender.setText("性别:" + strGender);
tvAge.setText("年龄:" + strAge);
tvPhone.setText("电话:" + strPhone);
tvEmail.setText("邮箱:" + strEmail);
tvHomePage.setText("主页:" + strHomePage);
tvMemo.setText("备注:" + strMemo);
}
}
}
我们的代码在这里就结束了,接下来是效果图,喜欢的朋友可以自己拿去改善,有不懂得还可以私聊我,我将会为你解答
软件界面
输入数据,这里的数据是我随便输入的
传送效果如图
好了,这个小案例到这里就结束了,感谢大家花时间来浏览,再见!