本文主要将android常见的几种UI集中在一起,主要是加强对常用UI的使用,以一个登录界面为例说明如下:
项目的布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/name"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/name"
android:textSize="@dimen/FontSize" />
<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:id="@+id/password"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/password"
android:textSize="@dimen/FontSize" />
<EditText
android:layout_width="@dimen/EditTextWidth"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/password"
android:layout_alignTop="@id/password"
android:inputType="textPassword"
android:id="@+id/passwordValue"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/age"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/age"
android:textSize="@dimen/FontSize" />
<!-- 将输入框限制为整数输入 -->
<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:id="@+id/sex"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/sex"
android:textSize="@dimen/FontSize" />
<RadioGroup
android:layout_toRightOf="@id/sex"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:id="@+id/sexmenu"
android:checkedButton="@+id/man"
>
<RadioButton
android:text="男"
android:id="@id/man"
/>
<RadioButton
android:text="女"
android:id="@+id/women"
/>
</RadioGroup>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/hobby"
android:textSize="@dimen/FontSize" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/hobby"
android:id="@+id/pingpang"
android:text="@string/pingpang"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pingpang"
android:id="@+id/basketball"
android:text="@string/basketball"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/hobby"
android:id="@+id/football"
android:text="@string/football"
android:layout_below="@id/pingpang"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/football"
android:id="@+id/tennis"
android:text="@string/tennis"
android:layout_alignTop="@id/football"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/city"
android:textSize="@dimen/FontSize" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/cityitem"
android:layout_toRightOf="@id/city"/>"
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
下面给出activity
package com.example.userlogin; import java.util.ArrayList; import android.R.anim; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; 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 MainActivity extends Activity { private static final String[] cites={"北京","上海","武汉","南京"}; EditText nameText,passText,ageText; Spinner spinner; RadioGroup radioGroup; CheckBox pingpang,basketball,tennis,football; Button regButton; ArrayList<CheckBox> arrayList; private boolean flag=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //定义一个arrayList来存放所有的CheckBox arrayList=new ArrayList<CheckBox>(); nameText=(EditText) findViewById(R.id.nameValue); passText=(EditText) findViewById(R.id.passwordValue); ageText=(EditText) findViewById(R.id.ageValue); regButton=(Button) findViewById(R.id.login_btn); spinner=(Spinner) findViewById(R.id.cityitem); radioGroup=(RadioGroup) findViewById(R.id.sexmenu); //将basketball对象添加到arrayList中去 basketball=(CheckBox) findViewById(R.id.basketball); arrayList.add(basketball); football=(CheckBox) findViewById(R.id.football); arrayList.add(football); tennis=(CheckBox) findViewById(R.id.tennis); arrayList.add(tennis); pingpang=(CheckBox) findViewById(R.id.pingpang); arrayList.add(pingpang); //创建数组型适配器 ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,cites ); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); regButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { flag=addUser(); if (flag) { AlertDialog.Builder dBuilder=new AlertDialog.Builder(MainActivity.this); dBuilder.setTitle("请确认信息") .setMessage("您的信息如下:"+"\n"+"姓名:"+nameText.getText().toString()+"\n" +"年龄:"+ageText.getText().toString()+"\n" +"性别:"+getSex()+"\n" +"爱好:"+getHobby()+"\n"+"城市:"+getCity()+"\n" ) .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ProgressDialog.show(MainActivity.this, "用户信息注册中", "请等待....").setCancelable(true); } }).setNegativeButton("修改", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel();//修改的时候删除对话框 } }).create().show(); } } }); } private String getSex(){ //获取被选中的单选框 RadioButton mRadioButton=(RadioButton) findViewById(radioGroup.getCheckedRadioButtonId()); return mRadioButton.getText().toString();//获取单选框的值 } private String getHobby(){ String string=""; for (CheckBox cb:arrayList) { if (cb.isChecked()) { string+=cb.getText().toString(); string+=","; } } if (string!="") { string=string.substring(0, string.length()-1); } else { string="您没有选择爱好!"; } return string; } private String getCity() { return cites[spinner.getSelectedItemPosition()];//获取当前选择的下拉列表的值 } /** * 拼写检测,检测输入内容是否符合要求 * */ public boolean addUser(){ if (nameText.getText().toString().length()==0) { nameText.setError("用户名不能为空"); return false; } if (passText.getText().toString().length()==0) { nameText.setError("密码不能为空"); return false; } if (ageText.getText().toString().length()==0) { nameText.setError("年龄不能为空"); return false; } return true; } }
<Button android:id="@+id/login_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="@string/login_btn" android:textSize="@dimen/FontSize" /> </RelativeLayout></LinearLayout>