Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP

上一篇文章我们学习了android通过findViewById的方式查找控件,本章将了解button控件,及btton如何绑定控件。

通过android的ui设计工具设计一个登录页面:

<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:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etPassword"
android:layout_alignParentLeft="true"
android:text="@string/main_activity_username" /> <TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etPassword"
android:layout_alignBottom="@+id/etPassword"
android:layout_alignLeft="@+id/tvUsername"
android:text="@string/main_activity_password" /> <EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="42dp"
android:layout_toRightOf="@+id/tvUsername"
android:hint="@string/main_activity_username_hint"
android:ems="10" /> <EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etUsername"
android:layout_below="@+id/etUsername"
android:layout_marginTop="41dp"
android:hint="@string/main_activity_password_hint"
android:ems="10"
android:inputType="textPassword" > <requestFocus />
</EditText> <Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnOk"
android:layout_alignRight="@+id/etUsername"
android:layout_marginRight="57dp"
android:text="@string/main_activity_btnCancel" /> <Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etPassword"
android:layout_marginRight="32dp"
android:layout_marginTop="59dp"
android:layout_toLeftOf="@+id/btnCancel"
android:text="@string/main_activity_btnOk" /> <TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPassword"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/etUsername"
android:layout_marginBottom="74dp"
android:text="" /> </RelativeLayout>

1)文本框使用的EditText控件;

2)文本hint是输入提示;

3)inputType是文本内容的格式设定。

后台给button绑定onClick事件:

android后台绑定button的onClick的函数setOnClickListener函数:

package com.example.helloword;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText etUsername, etPassword;
private TextView tvUsername, tvPassword, tvShow;
private Button btnOk, btnCancel; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); etUsername = (EditText) this.findViewById(R.id.etUsername);
etPassword = (EditText) this.findViewById(R.id.etPassword);
tvUsername = (TextView) this.findViewById(R.id.tvUsername);
tvPassword = (TextView) this.findViewById(R.id.tvPassword);
tvShow = (TextView) this.findViewById(R.id.tvShow);
btnOk = (Button) this.findViewById(R.id.btnOk);
btnCancel = (Button) this.findViewById(R.id.btnCancel); btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString(); tvShow.setText(username + "," + password);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
etUsername.setText("");
etPassword.setText("");
tvShow.setText("");
}
});
} @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;
}
}

当返回项目到手机页面时提示是否退出APP

    @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// 当点击回退时,弹出该窗口(也就相当于关闭操作)
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setTitle("是否退出?")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).setNegativeButton("取消", null).show();
return true;
}
return super.onKeyUp(keyCode, event);
}

使用android仿真机测试效果:

Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP

当点击返回到手机时:

Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP

上一篇:Android 模拟MotionEvent事件 触发输入法


下一篇:android 模拟按键事件