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;
    }
    
}
LoginActivity.java
复制代码

 

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;
    }
    
}
MainActivity.java
复制代码

 

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>
login.xml
复制代码

 

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>
activity_main.xml
复制代码

 

在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>
AndroidManifest.xml
复制代码

 

运行效果如下:

 

帐号密码均输入“123”,就可以成功登陆了。。开心^_^

posted on   乐之者v  阅读(1455)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示