package com.example.homework2; /** * @author 赵奇 */ import android.support.v7.app.ActionBarActivity; 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 ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView (R.layout.activity_main); } // button click event public void the Click (View veiw) { // set the original account and password String ID = "123456" ; String PassWord = "abc123" ; // get two the EditText the EditText the txt1 = (the EditText) the findViewById (R.id.et1); the EditText txt2 = (the EditText) the findViewById (R.id.et2); // Get button to the button but1 = (the button) the findViewById (R.id.bt1) ; // get the EditText text string and converted to type string S1 = txt1.getText () toString ().; string S2 =txt2.getText () toString ();. IF (ID.equals (s1) && PassWord.equals (s2)) { // landing successfully with toast Tip: successful landing . Toast.makeText (MainActivity the this , "login success" 0 ) the .Show (); } the else { // login fails with TextView Tip: login failed @ give TextView TextView TV1 = (TextView) the findViewById (R.id.tv3); // write TextView the "login failed" tv1.setText ( "login failed" ); // tv1.setText ( ""); // the try { // Thread.sleep(1000); // tv1.setVisibility(View.GONE); // // tv1.setText(""); // } catch (InterruptedException e) { // e.printStackTrace(); // } } } }
<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="com.example.homework2.MainActivity" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="90dp" android:text="账号" android:textSize="30sp" android:background="#FFFFFF" /> <EditText android:id="@+id/et1" android:layout_width="200sp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tv1" android:layout_alignParentRight="true" android:layout_alignTop="@+id/tv1" android:layout_toRightOf="@+id/tv1" android:phoneNumber="true" android:background="#FFFFFF" android:textSize="20sp" > </EditText> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="140sp" android:text="密码" android:textSize="30sp" android:background="#FFFFFF" /> <EditText android:id="@+id/et2" android:layout_width="200sp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tv2" android:layout_alignParentRight="true" android:layout_alignTop="@+id/tv2" android:layout_toRightOf="@+id/tv2" android:password="true" android:background="#FFFFFF" android:textSize="20sp" > </EditText> <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv2" android:layout_centerVertical="true" android:text="登录" android:textSize="30sp" android:background="#63B8FF" android:onClick="click" /> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/bt1" android:layout_centerHorizontal="true" android:layout_marginTop="98dp" android:textSize="30sp" /> <ImageView android:layout_width="45sp" android:layout_height="45sp" android:layout_above="@+id/et1" android:layout_centerHorizontal="true" android:src="@drawable/tp" android:layout_marginBottom="18dp" /> </RelativeLayout>