Android开发-简单的登陆界面

本文详细介绍了如何在Android应用中使用EditText、TextView、ImageButton组件实现账号密码输入,使用RadioGroup和RadioButton实现单选框功能,并通过Java代码进行事件绑定,最终构建了一个完整的登录界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用<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();
                 }
                
             }
         });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值