在Android中實現簡易的登入介面
阿新 • • 發佈:2019-02-14
Mainactivity佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
tools:context="com.example.administrator.taofirst.MainActivity">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="使用者名稱"
android:maxLength="10"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密碼">
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.ButtonBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陸"
android:layout_gravity="center"/>
<Button
android:id="@+id/bt_regist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="註冊"
android:layout_gravity="center"/>
</android.support.v7.widget.ButtonBarLayout>
</LinearLayout>
Mainactivity 的Java程式碼
package com.example.administrator.taofirst;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText=null;
EditText editText1=null;
Button button=null;
Button button1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.et_username);
editText1=(EditText) findViewById(R.id.et_password);
button=(Button) findViewById(R.id.bt_login);
button1=(Button) findViewById(R.id.bt_regist);
button.setOnClickListener(mylistener);
button1.setOnClickListener(mylistener);
}
private String[]ss=new String[2];
private View.OnClickListener mylistener =new View.OnClickListener() {
@Override
public void onClick(View v) {
String username=editText.getText().toString().trim();
String password=editText1.getText().toString().trim();
ss[0]=username;
ss[1]=password;
switch (v.getId())
{
case R.id.bt_login:
{
//Toast.makeText(MainActivity.this,"使用者名稱"+username+"-密碼"+password,Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("登入資訊");
builder.setItems(ss, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(MainActivity.this,"u",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
break;
}
case R.id.bt_regist:
{
Intent intent=new Intent();
intent.setClass(MainActivity.this,RedisrerActivity.class);
MainActivity.this.startActivity(intent);
break;
}
}
}};
}
著重強調在一個activity中呼叫另一個activity的方法
Intent intent=new Intent();
intent.setClass(MainActivity.this,RedisrerActivity.class);
MainActivity.this.startActivity(intent);//還可以用intent傳送資料