上一节: Android (安卓)本地化
上一节
下一节: Android (安卓)媒体播放器(MediaPlayer)
下一节
Android (安卓)登录界面
登录界面
登录应用程序是询问您的凭据以登录某些特定应用程序的屏幕。登录微信,QQ,淘宝等时,您可能已经看到了本章介绍了如何创建登录屏幕以及在进行错误尝试时如何管理安全性。首先,您必须定义两个TextView询问用户名和用户密码。密码TextView必须将inputType设置为password。其语法如下-
android:id = "@+id/editText2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:inputType = "textPassword" /> android:id = "@+id/editText1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> 定义一个带有登录文本的按钮并设置其onClick属性。之后,定义java文件中onClick属性中提到的函数。 android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:onClick = "login" android:text = "@string/Login" /> 在java文件中,在onClick方法内部,使用getText()和toString()方法获取用户名和密码文本,并使用equals()函数将其与文本匹配。 EditText username = (EditText)findViewById(R.id.editText1); EditText password = (EditText)findViewById(R.id.editText2); public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ //验证成功 登录 }else{ //验证错误处理 } 您需要做的最后一件事是提供一种安全机制,以便避免不必要的尝试。为此,初始化变量,并在每次错误尝试时将其减小。当它达到0时,禁用登录按钮。 int counter = 3; counter--; if(counter==0){ //关闭按钮,关闭应用程序 } 示例 这是演示登录应用程序的示例。它创建一个基本的应用程序,仅给您三种尝试登录应用程序的机会。 要试验该示例,您可以在实际设备或仿真器上运行它。 您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。 修改src/MainActivity.java文件以添加必要的代码。 修改res/layout/activity_main以添加相应的XML组件 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。 以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。 package com.jc2182.demo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button b1,b2; EditText ed1,ed2; TextView tx1; int counter = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(R.id.button); ed1 = (EditText)findViewById(R.id.editText); ed2 = (EditText)findViewById(R.id.editText2); b2 = (Button)findViewById(R.id.button2); tx1 = (TextView)findViewById(R.id.textView3); tx1.setVisibility(View.GONE); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ed1.getText().toString().equals("admin") && ed2.getText().toString().equals("admin")) { Toast.makeText(getApplicationContext(), "登录成功跳转...",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "登录验证失败",Toast.LENGTH_SHORT).show(); tx1.setVisibility(View.VISIBLE); tx1.setBackgroundColor(Color.RED); counter--; tx1.setText(Integer.toString(counter)); if (counter == 0) { b1.setEnabled(false); } } } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } 以下是res/layout/activity_main.xml文件的内容- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:text="登录界面示例" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> android:id="@+id/textView" android:layout_width="198dp" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:text="菜鸟教程" android:textColor="#ff7aff24" android:textSize="35dp" /> android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" /> 让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面- 尝试去登录 去尝试,去试错,查看效果 上一节: Android (安卓)本地化 上一节 下一节: Android (安卓)媒体播放器(MediaPlayer) 下一节 查看笔记 分享笔记 笔记内容: 称呼: Email: 站点: 分享笔记 重置 分类导航 前端 Ajax 教程 Angular 教程 Aurelia 教程 Bootstrap 教程 ChartJS 教程 CSS 教程 ES6 教程 FontAwesome 教程 HTML 教程 HTML 字符集 教程 HTML 游戏 教程 JavaScript 教程 jQuery 教程 Less 教程 React 教程 Sass 教程 Stylus 教程 TypeScript 教程 Unity 教程 Vue.js 教程 WebAssembly 教程 XAML 教程 颜色 教程 服务端 C# 教程 C++ 教程 COBOL 教程 C语言 教程 Fortran 教程 Go 教程 Groovy 教程 Java 教程 JSP 教程 JVM 教程 Kotlin 教程 Lisp 教程 Lua 教程 Node.js 教程 Pascal 教程 Perl 教程 PHP 教程 Python 教程 Python 3 教程 Ruby 教程 Rust 教程 Scala 教程 Spring 教程 Spring Boot 教程 Spring Cloud 教程 VB.Net 教程 移动端 Android 教程 IOS 教程 Objective-C 教程 React Native 教程 Swift 教程 小程序 教程 数据库 Access 教程 DB2 教程 Mariadb 教程 Memcached 教程 MongoDB 教程 MySQL 教程 Neo4j 教程 PL/SQL 教程 PostgreSQL 教程 Redis 教程 SQL 教程 SQL Server 教程 SQLite 教程 T-SQL 教程 数据格式 Jackson 教程 JSON 教程 SVG 教程 XML 教程 开发工具 ActiveMQ 教程 Ant 教程 Apache HttpClient 教程 Apache POI PPT 教程 AWS 教程 Docker 教程 ElasticSearch 教程 ExpressJS 教程 GIT 教程 GitLab 教程 Google Maps 教程 Gradle 教程 Java NIO 教程 JavaFX 教程 JavaMail 教程 JDBC 教程 jMeter 教程 JPA 教程 jsoup 教程 Junit 教程 KoaJS 教程 Kubernetes 教程 Log4j 教程 Logstash 教程 Lucene 教程 Makefile 教程 Maven 教程 RESTful 教程 Sed 教程 SEO 教程 Servlet 教程 SLF4J 教程 Socket.IO 教程 Struts 教程 SVN 教程 TestNG 教程 UML 教程 UNIX / LINUX 教程 WebSocket 教程 WPF 教程 xStream 教程 区块链 教程 数据处理 Flink 教程 Flume 教程 Hadoop 教程 Hbase 教程 Hive 教程 Kafka 教程 Kibana 教程 MapReduce 教程 MATLAB 教程 MyBatis 教程 Pig 教程 R语言 教程 Solr 教程 Spark 教程 Storm 教程 Zookeeper 教程 大数据分析 教程 数据仓库 教程 数据挖掘 教程 计算机基础 HTTP 教程 IPv4 教程 IPv6 教程 Ubantu 教程 WebServices 教程 嵌入式系统 教程 操作系统 教程 数据结构和算法 教程 汇编语言 教程 物联网 教程 电子电路基础 教程 编译器设计 教程 网站开发 教程 计算机 教程 计算机基础 教程 计算机网络 教程 设计模式 教程 AI CNTK 教程 Keras 教程 PyTorch 教程 TensorFlow 教程 人工智能 教程 机器学习 教程 Python 技术 Django 教程 Flask 教程 NumPy 教程 Pandas 教程 Pillow 教程 PyGTK 教程 PyQt5 教程 PySpark 教程 pytest 教程 Python -数据科学 教程 Python MySQL 教程 Python 取证 教程 Python 数据结构 教程 Python 文本处理 教程 Python 网络编程 教程 Python 网页抓取 教程 Python 设计模式 教程 RxPY 教程 SciPy 教程 Seaborn 教程 SymPy 教程 wxPython 教程 框架 Laravel 教程 Web 图标Icon 教程 Web2py 教程 WebGL 教程 WebRTC 教程 WordPress 教程 Yii 教程 Zend Framework 教程 SAP Crystal Reports 教程
android:id = "@+id/editText2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:inputType = "textPassword" />
android:id = "@+id/editText1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> 定义一个带有登录文本的按钮并设置其onClick属性。之后,定义java文件中onClick属性中提到的函数。 android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:onClick = "login" android:text = "@string/Login" /> 在java文件中,在onClick方法内部,使用getText()和toString()方法获取用户名和密码文本,并使用equals()函数将其与文本匹配。 EditText username = (EditText)findViewById(R.id.editText1); EditText password = (EditText)findViewById(R.id.editText2); public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ //验证成功 登录 }else{ //验证错误处理 } 您需要做的最后一件事是提供一种安全机制,以便避免不必要的尝试。为此,初始化变量,并在每次错误尝试时将其减小。当它达到0时,禁用登录按钮。 int counter = 3; counter--; if(counter==0){ //关闭按钮,关闭应用程序 } 示例 这是演示登录应用程序的示例。它创建一个基本的应用程序,仅给您三种尝试登录应用程序的机会。 要试验该示例,您可以在实际设备或仿真器上运行它。 您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。 修改src/MainActivity.java文件以添加必要的代码。 修改res/layout/activity_main以添加相应的XML组件 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。 以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。 package com.jc2182.demo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button b1,b2; EditText ed1,ed2; TextView tx1; int counter = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(R.id.button); ed1 = (EditText)findViewById(R.id.editText); ed2 = (EditText)findViewById(R.id.editText2); b2 = (Button)findViewById(R.id.button2); tx1 = (TextView)findViewById(R.id.textView3); tx1.setVisibility(View.GONE); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ed1.getText().toString().equals("admin") && ed2.getText().toString().equals("admin")) { Toast.makeText(getApplicationContext(), "登录成功跳转...",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "登录验证失败",Toast.LENGTH_SHORT).show(); tx1.setVisibility(View.VISIBLE); tx1.setBackgroundColor(Color.RED); counter--; tx1.setText(Integer.toString(counter)); if (counter == 0) { b1.setEnabled(false); } } } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } 以下是res/layout/activity_main.xml文件的内容- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:text="登录界面示例" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> android:id="@+id/textView" android:layout_width="198dp" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:text="菜鸟教程" android:textColor="#ff7aff24" android:textSize="35dp" /> android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" /> 让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面- 尝试去登录 去尝试,去试错,查看效果 上一节: Android (安卓)本地化 上一节 下一节: Android (安卓)媒体播放器(MediaPlayer) 下一节 查看笔记 分享笔记 笔记内容: 称呼: Email: 站点: 分享笔记 重置 分类导航 前端 Ajax 教程 Angular 教程 Aurelia 教程 Bootstrap 教程 ChartJS 教程 CSS 教程 ES6 教程 FontAwesome 教程 HTML 教程 HTML 字符集 教程 HTML 游戏 教程 JavaScript 教程 jQuery 教程 Less 教程 React 教程 Sass 教程 Stylus 教程 TypeScript 教程 Unity 教程 Vue.js 教程 WebAssembly 教程 XAML 教程 颜色 教程 服务端 C# 教程 C++ 教程 COBOL 教程 C语言 教程 Fortran 教程 Go 教程 Groovy 教程 Java 教程 JSP 教程 JVM 教程 Kotlin 教程 Lisp 教程 Lua 教程 Node.js 教程 Pascal 教程 Perl 教程 PHP 教程 Python 教程 Python 3 教程 Ruby 教程 Rust 教程 Scala 教程 Spring 教程 Spring Boot 教程 Spring Cloud 教程 VB.Net 教程 移动端 Android 教程 IOS 教程 Objective-C 教程 React Native 教程 Swift 教程 小程序 教程 数据库 Access 教程 DB2 教程 Mariadb 教程 Memcached 教程 MongoDB 教程 MySQL 教程 Neo4j 教程 PL/SQL 教程 PostgreSQL 教程 Redis 教程 SQL 教程 SQL Server 教程 SQLite 教程 T-SQL 教程 数据格式 Jackson 教程 JSON 教程 SVG 教程 XML 教程 开发工具 ActiveMQ 教程 Ant 教程 Apache HttpClient 教程 Apache POI PPT 教程 AWS 教程 Docker 教程 ElasticSearch 教程 ExpressJS 教程 GIT 教程 GitLab 教程 Google Maps 教程 Gradle 教程 Java NIO 教程 JavaFX 教程 JavaMail 教程 JDBC 教程 jMeter 教程 JPA 教程 jsoup 教程 Junit 教程 KoaJS 教程 Kubernetes 教程 Log4j 教程 Logstash 教程 Lucene 教程 Makefile 教程 Maven 教程 RESTful 教程 Sed 教程 SEO 教程 Servlet 教程 SLF4J 教程 Socket.IO 教程 Struts 教程 SVN 教程 TestNG 教程 UML 教程 UNIX / LINUX 教程 WebSocket 教程 WPF 教程 xStream 教程 区块链 教程 数据处理 Flink 教程 Flume 教程 Hadoop 教程 Hbase 教程 Hive 教程 Kafka 教程 Kibana 教程 MapReduce 教程 MATLAB 教程 MyBatis 教程 Pig 教程 R语言 教程 Solr 教程 Spark 教程 Storm 教程 Zookeeper 教程 大数据分析 教程 数据仓库 教程 数据挖掘 教程 计算机基础 HTTP 教程 IPv4 教程 IPv6 教程 Ubantu 教程 WebServices 教程 嵌入式系统 教程 操作系统 教程 数据结构和算法 教程 汇编语言 教程 物联网 教程 电子电路基础 教程 编译器设计 教程 网站开发 教程 计算机 教程 计算机基础 教程 计算机网络 教程 设计模式 教程 AI CNTK 教程 Keras 教程 PyTorch 教程 TensorFlow 教程 人工智能 教程 机器学习 教程 Python 技术 Django 教程 Flask 教程 NumPy 教程 Pandas 教程 Pillow 教程 PyGTK 教程 PyQt5 教程 PySpark 教程 pytest 教程 Python -数据科学 教程 Python MySQL 教程 Python 取证 教程 Python 数据结构 教程 Python 文本处理 教程 Python 网络编程 教程 Python 网页抓取 教程 Python 设计模式 教程 RxPY 教程 SciPy 教程 Seaborn 教程 SymPy 教程 wxPython 教程 框架 Laravel 教程 Web 图标Icon 教程 Web2py 教程 WebGL 教程 WebRTC 教程 WordPress 教程 Yii 教程 Zend Framework 教程 SAP Crystal Reports 教程
android:id = "@+id/editText1"
/>
定义一个带有登录文本的按钮并设置其onClick属性。之后,定义java文件中onClick属性中提到的函数。
android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:onClick = "login" android:text = "@string/Login" /> 在java文件中,在onClick方法内部,使用getText()和toString()方法获取用户名和密码文本,并使用equals()函数将其与文本匹配。 EditText username = (EditText)findViewById(R.id.editText1); EditText password = (EditText)findViewById(R.id.editText2); public void login(View view){ if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ //验证成功 登录 }else{ //验证错误处理 } 您需要做的最后一件事是提供一种安全机制,以便避免不必要的尝试。为此,初始化变量,并在每次错误尝试时将其减小。当它达到0时,禁用登录按钮。 int counter = 3; counter--; if(counter==0){ //关闭按钮,关闭应用程序 } 示例 这是演示登录应用程序的示例。它创建一个基本的应用程序,仅给您三种尝试登录应用程序的机会。 要试验该示例,您可以在实际设备或仿真器上运行它。 您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。 修改src/MainActivity.java文件以添加必要的代码。 修改res/layout/activity_main以添加相应的XML组件 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。 以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。 package com.jc2182.demo; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button b1,b2; EditText ed1,ed2; TextView tx1; int counter = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(R.id.button); ed1 = (EditText)findViewById(R.id.editText); ed2 = (EditText)findViewById(R.id.editText2); b2 = (Button)findViewById(R.id.button2); tx1 = (TextView)findViewById(R.id.textView3); tx1.setVisibility(View.GONE); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ed1.getText().toString().equals("admin") && ed2.getText().toString().equals("admin")) { Toast.makeText(getApplicationContext(), "登录成功跳转...",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "登录验证失败",Toast.LENGTH_SHORT).show(); tx1.setVisibility(View.VISIBLE); tx1.setBackgroundColor(Color.RED); counter--; tx1.setText(Integer.toString(counter)); if (counter == 0) { b1.setEnabled(false); } } } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } 以下是res/layout/activity_main.xml文件的内容- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:text="登录界面示例" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> android:id="@+id/textView" android:layout_width="198dp" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:text="菜鸟教程" android:textColor="#ff7aff24" android:textSize="35dp" /> android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" /> 让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面- 尝试去登录 去尝试,去试错,查看效果 上一节: Android (安卓)本地化 上一节 下一节: Android (安卓)媒体播放器(MediaPlayer) 下一节 查看笔记 分享笔记 笔记内容: 称呼: Email: 站点: 分享笔记 重置 分类导航 前端 Ajax 教程 Angular 教程 Aurelia 教程 Bootstrap 教程 ChartJS 教程 CSS 教程 ES6 教程 FontAwesome 教程 HTML 教程 HTML 字符集 教程 HTML 游戏 教程 JavaScript 教程 jQuery 教程 Less 教程 React 教程 Sass 教程 Stylus 教程 TypeScript 教程 Unity 教程 Vue.js 教程 WebAssembly 教程 XAML 教程 颜色 教程 服务端 C# 教程 C++ 教程 COBOL 教程 C语言 教程 Fortran 教程 Go 教程 Groovy 教程 Java 教程 JSP 教程 JVM 教程 Kotlin 教程 Lisp 教程 Lua 教程 Node.js 教程 Pascal 教程 Perl 教程 PHP 教程 Python 教程 Python 3 教程 Ruby 教程 Rust 教程 Scala 教程 Spring 教程 Spring Boot 教程 Spring Cloud 教程 VB.Net 教程 移动端 Android 教程 IOS 教程 Objective-C 教程 React Native 教程 Swift 教程 小程序 教程 数据库 Access 教程 DB2 教程 Mariadb 教程 Memcached 教程 MongoDB 教程 MySQL 教程 Neo4j 教程 PL/SQL 教程 PostgreSQL 教程 Redis 教程 SQL 教程 SQL Server 教程 SQLite 教程 T-SQL 教程 数据格式 Jackson 教程 JSON 教程 SVG 教程 XML 教程 开发工具 ActiveMQ 教程 Ant 教程 Apache HttpClient 教程 Apache POI PPT 教程 AWS 教程 Docker 教程 ElasticSearch 教程 ExpressJS 教程 GIT 教程 GitLab 教程 Google Maps 教程 Gradle 教程 Java NIO 教程 JavaFX 教程 JavaMail 教程 JDBC 教程 jMeter 教程 JPA 教程 jsoup 教程 Junit 教程 KoaJS 教程 Kubernetes 教程 Log4j 教程 Logstash 教程 Lucene 教程 Makefile 教程 Maven 教程 RESTful 教程 Sed 教程 SEO 教程 Servlet 教程 SLF4J 教程 Socket.IO 教程 Struts 教程 SVN 教程 TestNG 教程 UML 教程 UNIX / LINUX 教程 WebSocket 教程 WPF 教程 xStream 教程 区块链 教程 数据处理 Flink 教程 Flume 教程 Hadoop 教程 Hbase 教程 Hive 教程 Kafka 教程 Kibana 教程 MapReduce 教程 MATLAB 教程 MyBatis 教程 Pig 教程 R语言 教程 Solr 教程 Spark 教程 Storm 教程 Zookeeper 教程 大数据分析 教程 数据仓库 教程 数据挖掘 教程 计算机基础 HTTP 教程 IPv4 教程 IPv6 教程 Ubantu 教程 WebServices 教程 嵌入式系统 教程 操作系统 教程 数据结构和算法 教程 汇编语言 教程 物联网 教程 电子电路基础 教程 编译器设计 教程 网站开发 教程 计算机 教程 计算机基础 教程 计算机网络 教程 设计模式 教程 AI CNTK 教程 Keras 教程 PyTorch 教程 TensorFlow 教程 人工智能 教程 机器学习 教程 Python 技术 Django 教程 Flask 教程 NumPy 教程 Pandas 教程 Pillow 教程 PyGTK 教程 PyQt5 教程 PySpark 教程 pytest 教程 Python -数据科学 教程 Python MySQL 教程 Python 取证 教程 Python 数据结构 教程 Python 文本处理 教程 Python 网络编程 教程 Python 网页抓取 教程 Python 设计模式 教程 RxPY 教程 SciPy 教程 Seaborn 教程 SymPy 教程 wxPython 教程 框架 Laravel 教程 Web 图标Icon 教程 Web2py 教程 WebGL 教程 WebRTC 教程 WordPress 教程 Yii 教程 Zend Framework 教程 SAP Crystal Reports 教程
android:id = "@+id/button1"
android:onClick = "login"
android:text = "@string/Login"
在java文件中,在onClick方法内部,使用getText()和toString()方法获取用户名和密码文本,并使用equals()函数将其与文本匹配。
EditText username = (EditText)findViewById(R.id.editText1);
EditText password = (EditText)findViewById(R.id.editText2);
public void login(View view){
if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){
//验证成功 登录
}else{
//验证错误处理
}
您需要做的最后一件事是提供一种安全机制,以便避免不必要的尝试。为此,初始化变量,并在每次错误尝试时将其减小。当它达到0时,禁用登录按钮。
int counter = 3;
counter--;
if(counter==0){
//关闭按钮,关闭应用程序
示例
这是演示登录应用程序的示例。它创建一个基本的应用程序,仅给您三种尝试登录应用程序的机会。 要试验该示例,您可以在实际设备或仿真器上运行它。
您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。
修改src/MainActivity.java文件以添加必要的代码。
修改res/layout/activity_main以添加相应的XML组件
运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。
以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。
package com.jc2182.demo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
ed1 = (EditText)findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "登录成功跳转...",Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "登录验证失败",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
});
b2.setOnClickListener(new View.OnClickListener() {
finish();
以下是res/layout/activity_main.xml文件的内容-
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> android:text="登录界面示例" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> android:id="@+id/textView" android:layout_width="198dp" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:text="菜鸟教程" android:textColor="#ff7aff24" android:textSize="35dp" /> android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" />
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:text="登录界面示例" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> android:id="@+id/textView" android:layout_width="198dp" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:text="菜鸟教程" android:textColor="#ff7aff24" android:textSize="35dp" /> android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" />
android:text="登录界面示例"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
android:id="@+id/textView" android:layout_width="198dp" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:text="菜鸟教程" android:textColor="#ff7aff24" android:textSize="35dp" /> android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" />
android:id="@+id/textView"
android:layout_width="198dp"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:text="菜鸟教程"
android:textColor="#ff7aff24"
android:textSize="35dp" />
android:id="@+id/button" android:layout_width="94dp" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:text="下载" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" />
android:id="@+id/button"
android:layout_width="94dp"
android:layout_below="@+id/imageView"
android:text="下载" />
android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/logo" android:background="#11EE22bb" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" />
android:id="@+id/imageView"
android:src="@drawable/logo"
android:background="#11EE22bb"
android:layout_below="@+id/textView"
android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="272dp" android:layout_height="362dp" android:layout_below="@+id/button" android:layout_alignStart="@+id/textview" android:layout_alignLeft="@+id/textview" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_alignParentBottom="true" android:layout_marginEnd="-30dp" android:layout_marginRight="-30dp" android:layout_marginBottom="7dp" android:progressDrawable="@drawable/circular_progress_bar" />
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="272dp"
android:layout_height="362dp"
android:layout_below="@+id/button"
android:layout_alignStart="@+id/textview"
android:layout_alignLeft="@+id/textview"
android:layout_alignEnd="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-30dp"
android:layout_marginRight="-30dp"
android:layout_marginBottom="7dp"
android:progressDrawable="@drawable/circular_progress_bar" />
让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面-
尝试去登录 去尝试,去试错,查看效果
查看笔记 分享笔记
笔记内容:
称呼:
Email:
站点:
分享笔记 重置
分类导航
前端
Ajax 教程
Angular 教程
Aurelia 教程
Bootstrap 教程
ChartJS 教程
CSS 教程
ES6 教程
FontAwesome 教程
HTML 教程
HTML 字符集 教程
HTML 游戏 教程
JavaScript 教程
jQuery 教程
Less 教程
React 教程
Sass 教程
Stylus 教程
TypeScript 教程
Unity 教程
Vue.js 教程
WebAssembly 教程
XAML 教程
颜色 教程
服务端
C# 教程
C++ 教程
COBOL 教程
C语言 教程
Fortran 教程
Go 教程
Groovy 教程
Java 教程
JSP 教程
JVM 教程
Kotlin 教程
Lisp 教程
Lua 教程
Node.js 教程
Pascal 教程
Perl 教程
PHP 教程
Python 教程
Python 3 教程
Ruby 教程
Rust 教程
Scala 教程
Spring 教程
Spring Boot 教程
Spring Cloud 教程
VB.Net 教程
移动端
Android 教程
IOS 教程
Objective-C 教程
React Native 教程
Swift 教程
小程序 教程
数据库
Access 教程
DB2 教程
Mariadb 教程
Memcached 教程
MongoDB 教程
MySQL 教程
Neo4j 教程
PL/SQL 教程
PostgreSQL 教程
Redis 教程
SQL 教程
SQL Server 教程
SQLite 教程
T-SQL 教程
数据格式
Jackson 教程
JSON 教程
SVG 教程
XML 教程
开发工具
ActiveMQ 教程
Ant 教程
Apache HttpClient 教程
Apache POI PPT 教程
AWS 教程
Docker 教程
ElasticSearch 教程
ExpressJS 教程
GIT 教程
GitLab 教程
Google Maps 教程
Gradle 教程
Java NIO 教程
JavaFX 教程
JavaMail 教程
JDBC 教程
jMeter 教程
JPA 教程
jsoup 教程
Junit 教程
KoaJS 教程
Kubernetes 教程
Log4j 教程
Logstash 教程
Lucene 教程
Makefile 教程
Maven 教程
RESTful 教程
Sed 教程
SEO 教程
Servlet 教程
SLF4J 教程
Socket.IO 教程
Struts 教程
SVN 教程
TestNG 教程
UML 教程
UNIX / LINUX 教程
WebSocket 教程
WPF 教程
xStream 教程
区块链 教程
数据处理
Flink 教程
Flume 教程
Hadoop 教程
Hbase 教程
Hive 教程
Kafka 教程
Kibana 教程
MapReduce 教程
MATLAB 教程
MyBatis 教程
Pig 教程
R语言 教程
Solr 教程
Spark 教程
Storm 教程
Zookeeper 教程
大数据分析 教程
数据仓库 教程
数据挖掘 教程
计算机基础
HTTP 教程
IPv4 教程
IPv6 教程
Ubantu 教程
WebServices 教程
嵌入式系统 教程
操作系统 教程
数据结构和算法 教程
汇编语言 教程
物联网 教程
电子电路基础 教程
编译器设计 教程
网站开发 教程
计算机 教程
计算机基础 教程
计算机网络 教程
设计模式 教程
AI
CNTK 教程
Keras 教程
PyTorch 教程
TensorFlow 教程
人工智能 教程
机器学习 教程
Python 技术
Django 教程
Flask 教程
NumPy 教程
Pandas 教程
Pillow 教程
PyGTK 教程
PyQt5 教程
PySpark 教程
pytest 教程
Python -数据科学 教程
Python MySQL 教程
Python 取证 教程
Python 数据结构 教程
Python 文本处理 教程
Python 网络编程 教程
Python 网页抓取 教程
Python 设计模式 教程
RxPY 教程
SciPy 教程
Seaborn 教程
SymPy 教程
wxPython 教程
框架
Laravel 教程
Web 图标Icon 教程
Web2py 教程
WebGL 教程
WebRTC 教程
WordPress 教程
Yii 教程
Zend Framework 教程
SAP
Crystal Reports 教程
李清照《如梦令》原文|赏析|翻译|注释
知城城市指标
2022世界杯直播平台盘点:央视、腾讯、爱奇艺及优酷全攻略!
乌江在哪里(贵州乌江在哪里)
卫生巾使用方法教程(女性必知 卫生巾正确使用手册)
LOL新英雄什么时候降价 LOL新英雄价格和降价机制攻略
抖音:2022 世界杯累计直播观看人次达 106 亿,决赛观看人数 2.3 亿
世界各国英文缩写
如何校准电子血压计
销项是负数应该怎么进行纳税申报