package com.example.administrator.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); /* 按钮提示 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(),"你点击了登录按钮1",Toast.LENGTH_LONG).show(); } });*/ //button.setOnClickListener((View.OnClickListener) MainActivity.this); button.setOnClickListener(clickListener); } View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你点击了登录按钮1", Toast.LENGTH_LONG).show(); } }; }
<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" tools:context="com.example.administrator.myapplication.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28dp" android:text="欢迎使用该应用" android:layout_gravity="center" android:autoLink="web"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="请输入帐号:"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="请输入密码:"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF4801" android:text="登录" android:id="@+id/button"/> </LinearLayout>