<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent"> <TextView android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户登录界面" android:textAlignment="center" android:textSize="24sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名" android:textAlignment="center" android:textSize="24sp" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的用户名"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码" android:textAlignment="center" android:textSize="24sp" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的密码"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent"> <Button android:id="@+id/login" android:layout_height="60dp" android:layout_width="wrap_content" android:text="登录" android:layout_gravity="center" android:textAlignment="center" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
package com.example.dl; import android.os.Bundle; import android.app.Activity; import android.text.method.KeyListener; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public abstract class MainActivity extends Activity implements KeyListener { Button button; EditText username; EditText password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.action_settings); username = (EditText)findViewById(R.id.action_settings); password = (EditText)findViewById(R.id.action_settings); button.setKeyListener(this); } public void onClick(View v) { String username1 = username.getText().toString(); String password1 = password.getText().toString(); String ok = "登录成功"; String fail = "登录失败"; if (username1.equals("lhk") && password1.equals("123456")) { Toast.makeText(MainActivity.this,ok,Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MainActivity.this,fail,Toast.LENGTH_SHORT).show(); } } protected void onCreate1(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.main, menu); return true; } }