文本框常用屬性
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);
}
}
}
我們的代碼在這裡就結束了,接下來是效果圖,喜歡的朋友可以自己拿去改善,有不懂得還可以私聊我,我将會為你解答
軟體界面
輸入資料,這裡的資料是我随便輸入的
傳送效果如圖
好了,這個小案例到這裡就結束了,感謝大家花時間來浏覽,再見!