展会信息港展会大全

一个Android用户注册的Activity实例
来源:互联网   发布日期:2015-09-28 16:19:53   浏览:2259次  

导读:1、UserLogin的Activitypackage cn.dccssq; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialo......

1、UserLogin的Activity

package cn.dccssq;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Spinner;

public class UserLogin extends Activity {

private static final String[] citites = { "Beijing", "Tokyo", "ATown",

"BTower" };

private EditText name, age, pass;

private Button regBtn;

private RadioGroup sextRdoGrp;

private CheckBox baseball, basketball, football, tennis;

private Spinner citySpinner;

private boolean flag = true;

private List<CHECKBOX> favorites;

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

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

favorites = new ArrayList<CHECKBOX>();

name = (EditText) findViewById(R.id.nameValue);

age = (EditText) findViewById(R.id.ageValue);

pass = (EditText) findViewById(R.id.passValue);

regBtn = (Button) findViewById(R.id.registerButton);

citySpinner = (Spinner) findViewById(R.id.cityItems);

sextRdoGrp = (RadioGroup) findViewById(R.id.sexMenu);

baseball = (CheckBox) findViewById(R.id.checkboxbaseball);

favorites.add(baseball);

basketball = (CheckBox) findViewById(R.id.checkboxbasketball);

favorites.add(basketball);

football = (CheckBox) findViewById(R.id.checkboxfootball);

favorites.add(football);

tennis = (CheckBox) findViewById(R.id.checkboxtennis);

favorites.add(tennis);

ArrayAdapter<STRING> adapter = new ArrayAdapter<STRING>(UserLogin.this,

android.R.layout.simple_spinner_item, citites);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

citySpinner.setAdapter(adapter);

regBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

flag = addUser();

if (flag) {

new AlertDialog.Builder(UserLogin.this)

.setTitle("Please Confirm the Information:")

.setMessage(

"Your Information:" + "/n" + "Name:"

+ name.getText().toString() + "/n"

+ "Age:" + age.getText().toString()

+ "/n" + "Sex:" + getSex() + "/n"

+ "Favorite:" + getFavorite()

+ "/n" + "City:" + getCity() + "/n")

.setCancelable(false)

.setPositiveButton("Confirm",

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface arg0, int arg1) {

// TODO Auto-generated method stub

ProgressDialog.show(UserLogin.this,

"Signing",

"Please wait.....")

.setCancelable(true);

}

})

.setNegativeButton("Edit",

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface arg0, int arg1) {

// TODO Auto-generated method stub

arg0.cancel();

}

}).show();

}

}

});

}

private String getCity() {

return citites[citySpinner.getSelectedItemPosition()];

}

private String getFavorite() {

String favString = "";

for (CheckBox cb : favorites) {

if (cb.isChecked()) {

favString += cb.getText().toString();

favString += ",";

}

}

if (favString != "") {

favString = favString.substring(0, favString.length() - 1);

} else {

favString = "Please Select your favorite!";

}

return favString;

}

private String getSex() {

RadioButton mRadio = (RadioButton) findViewById(sextRdoGrp

.getCheckedRadioButtonId());

return mRadio.getText().toString();

}

public boolean addUser() {

if (name.getText().toString().length() == 0) {

name.setError("Please input your name!");

return false;

}

if (age.getText().toString().length() == 0) {

age.setError("Please input your age!");

return false;

}

if (pass.getText().toString().length() == 0) {

pass.setError("Please input the password!");

return false;

}

return true;

}

}

2、main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/nameString"

android:textSize="@dimen/fontSize"

android:id="@+id/name"

/>

<EditText

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/name"

android:layout_alignTop="@id/name"

android:id="@+id/nameValue"

/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/passString"

android:textSize="@dimen/fontSize"

android:id="@+id/pass"

/>

<EditText

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/pass"

android:layout_alignTop="@id/pass"

android:password="true"

android:id="@+id/passValue"

/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/ageString"

android:textSize="@dimen/fontSize"

android:id="@+id/age"

/>

<EditText

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/age"

android:layout_alignTop="@id/age"

android:numeric="integer"

android:id="@+id/ageValue"

/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/sexString"

android:textSize="@dimen/fontSize"

android:id="@+id/sex"/>

<RadioGroup

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/sex"

android:checkedButton="@+id/radioMan"

android:orientation="horizontal"

android:id="@+id/sexMenu">

<RadioButton android:text="@string/male" android:id="@id/radioMan"/>

<RadioButton android:text="@string/Female" android:id="@+id/radioWoman"/>

</RadioGroup>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/favoriteString"

android:textSize="@dimen/fontSize"

android:id="@+id/favorite"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/favorite"

android:text="@string/baseball"

android:id="@+id/checkboxbaseball"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/checkboxbaseball"

android:text="@string/football"

android:id="@+id/checkboxfootball"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/favorite"

android:layout_below="@id/checkboxfootball"

android:text="@string/basketball"

android:id="@+id/checkboxbasketball"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/checkboxbasketball"

android:layout_alignTop="@id/checkboxbasketball"

android:text="@string/tennis"

android:id="@+id/checkboxtennis"/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/cityString"

android:textSize="@dimen/fontSize"

android:id="@+id/city"/>

<Spinner

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/city"

android:id="@+id/cityItems">

</Spinner>

</RelativeLayout>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/registerButtonText"

android:id="@+id/registerButton"/>

</LinearLayout>

3、strings.xml

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

<resources>

<string name="hello">Hello World, LoginActivity!</string>

<string name="app_name">User Login</string>

<string name="nameString">User:</string>

<string name="ageString">Age:</string>

<string name="registerButtonText">Sign</string>

<string name="sexString">Sex:</string>

<string name="favoriteString">Favorite:</string>

<string name="cityString">City:</string>

<string name="passString">Password:</string>

<string name="baseball">Baseball</string>

<string name="basketball">Basketball</string>

<string name="football">Football</string>

<string name="tennis">Tennis</string>

<string name="male">Male</string>

<string name="Female">Female</string>

</resources>

4、parameters.xml

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

<resources>

<dimen name="fontSize">22px</dimen>

<dimen name="TextViewWidth">90px</dimen>

<dimen name="EditTextWidth">160px</dimen>

</resources>

赞助本站

人工智能实验室

相关热词: Activity 用户注册 实例

相关内容
AiLab云推荐
展开

热门栏目HotCates

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