Sometimes, you need to validate text input by user when the EditText lost focus. To detect when the EditText lost focus, you need to use the setOnFocusChangeListener(OnFocusChangeListener l) method. You pass an object of anonymous class that implements the OnFocusChangeListener interface. You override the onFocusChange(View v, boolean hasFocus) method to perform data validation. The v parameter is the view that generates the focus change event. The hasFocus parameter is true or false. It is true when the view is focused and it is false when the view lost focus.
The example application below displays an input box when the user name or password text box is leaving blank when it lost focus.
In the activity_main.xml file, you add one TextView, two EditTexts, and one Button as shown below.
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Log in"
android:gravity="center_horizontal"
android:textStyle="bold"
/>
<EditText
android:id="@+id/txtusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User name"
android:inputType="text"
/>
<EditText
android:id="@+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="16sp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<EditText
android:id="@+id/txtinput"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:inputType="numberSigned"
/>
<Button
android:id="@+id/btdone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtUserName=(EditText)findViewById(R.id.txtusername);
final EditText txtPassword=(EditText)findViewById(R.id.txtpassword);
txtUserName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
EditText txtUser=(EditText)v;
String userName=txtUser.getText().toString();
if(userName.trim().equals("")){
showInputBox(MainActivity.this, "Please input user name",txtUser);
}
}
}
});
txtPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
EditText txtPwd=(EditText)v;
String password=txtPwd.getText().toString();
if(password.trim().equals("")){
showInputBox(MainActivity.this, "Please input password",txtPwd);
}
}
}
});
}
public void showInputBox(final Context context,String message,final EditText txt){
// Create dialog instance
final Dialog dialog = new Dialog(context);
// Set dialog title
dialog.setTitle("Input box");
// Set dialog layout
dialog.setContentView(R.layout.input_box);
TextView tvMess=(TextView)dialog.findViewById(R.id.txtmessage);
tvMess.setText(message);
tvMess.setTextColor(Color.parseColor("#ff2222"));
Button btDone=(Button)dialog.findViewById(R.id.btdone);
final EditText txtInput=(EditText)dialog.findViewById(R.id.txtinput);
txtInput.setText(txt.getText().toString());
txtInput.setInputType(txt.getInputType());
btDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txtInput.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
}
Free & Easy to use app for Android
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.