您的位置:首页 > 移动开发 > Android开发

Android开发-简单的登陆界面

2016-04-06 22:14 495 查看
使用<EditText/><TextView><ImageButton/>组件xml中开发界面;使用findViewById、OnClickListener后台绑定事件:

使用<RadioGroup>,<RadioButton>组件实现单选框

具体代码如下:

xml代码:

<?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:orientation="vertical" >

<!-- 账号输入 -->

<TextView

android:id="@+id/text01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/user"

/>

<EditText

android:id="@+id/edt01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="@string/input"

android:textSize="15sp"

android:layout_marginTop="20dp"

/>

<!-- 密码输入 -->

<TextView

android:id="@+id/text02"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/password"

/>

<EditText

android:id="@+id/edt02"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="@string/passw"

android:textSize="15sp"

android:layout_marginTop="20dp"

android:inputType="numberPassword"

/>

<!-- 单选框应用 -->

<RadioGroup

android:id="@+id/group"

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

<RadioButton

android:id="@+id/rb1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/man"

/>

<RadioButton

android:id="@+id/rb2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/woman"

android:checked="true"

/>

</RadioGroup>

<!-- 登陆按钮 -->

<Button

android:id="@+id/submit_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/submit"

android:layout_gravity="center"

android:background="@drawable/anydo"

/>

</LinearLayout>

java代码:

package com.example.login;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText ed01;

private EditText ed02;

private Button submit;

private String results1;

private String results2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//获取界布局文件中的输入框、按钮、文本框控件

ed01 = (EditText) this.findViewById(R.id.edt01);

ed02 = (EditText) this.findViewById(R.id.edt02);

submit = (Button) this.findViewById(R.id.submit_btn);

//为按钮添加点击事件

submit.setOnClickListener(new OnClickListener(){

public void onClick(View v){

//获取输入框中输入的内容

results1 =ed01.getText().toString();

results2 = ed02.getText().toString();

//输入的账号密码判等;

if(results1.equals("android")&&results2.equals("2016")){

Toast.makeText(MainActivity.this, R.string.login,Toast.LENGTH_LONG).show();

}else{

Toast.makeText(MainActivity.this, R.string.failed,Toast.LENGTH_LONG).show();

}

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: