android示例:一个简单的登陆程序
最近写了个简单的登陆程序,有几点收获:
1.懂得如何在LinearLayout中嵌套LinearLayout,完善布局的行列;
2.用android:layout_weight控制控件的比重;
3.用getText()获取EditText内容;
4.熟悉控件的编写,不用再照着书抄写了=.=
代码如下:
LoginActivity.java

import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity { private Button button; private EditText editText1; private EditText editText2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); //加载按钮和文件框等控件 button=(Button)findViewById(R.id.button); editText1=(EditText)findViewById(R.id.input_account); editText2=(EditText)findViewById(R.id.input_password); button.setOnClickListener(new OnClickListener(){ public void onClick(View v){ //获取editText的文本内容,并删去空格 String inputAccount=editText1.getText().toString().trim(); String inputPassword=editText2.getText().toString().trim(); //如果账号密码为"123"就跳转活动 if(inputAccount.equals("123")&& inputPassword.equals("123")) { Toast.makeText(LoginActivity.this, "账号密码正确,正在登陆中", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); }else { Toast.makeText(LoginActivity.this, "账号或者密码不正确,请重新输入", Toast.LENGTH_SHORT).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.setting, menu); return true; } }
MainActivity.java

import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.setting, menu); return true; } }
login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".LoginActivity" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帐号:" /> <EditText android:id="@+id/input_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Input your account" android:layout_weight="1" > </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:id="@+id/input_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Input your password" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登陆" /> </LinearLayout> </LinearLayout>
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆成功!撒花~" /> </RelativeLayout>
在AndroidManifest.xml中注册Activity

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.logindemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.logindemo.LoginActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.logindemo.MainActivity" > </activity> </application> </manifest>
运行效果如下:
帐号密码均输入“123”,就可以成功登陆了。。开心^_^
分类:
其他知识--安卓开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了