android樣本:一個簡單的登陸程式

來源:互聯網
上載者:User

標籤:

最近寫了個簡單的登陸程式,有幾點收穫:

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”,就可以成功登陸了。。開心^_^

android樣本:一個簡單的登陸程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.