res/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_text"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lock/Unlock" />
<TextView
android:id="@+id/txtview"
android:layout_width="fill_parent"
android:textSize="25.0dp"
android:gravity="center"
android:layout_height="wrap_content"
android:text="InputFilter" />
</LinearLayout>
InputFilterActivity.java:
public class InputFilterActivity extends Activity {
private EditText editText;
private boolean value = false;
Button b;
String str = "Hello,Android!";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText=(EditText)findViewById(R.id.edit_text);
editText.setText("EditText component");
b = (Button) findViewById(R.id.btnId);
b.setText("Lock");
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (value) {
value = false;
b.setText("Lock");
} else {
value = true;
b.setText("Unlock");
}
lockUnlock(value);
}
});
}
private void lockUnlock(boolean value) {
if (value) {
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
return source.length() < 1 ? dest.subSequence(dstart, dend)
: "";
}
} });
} else {
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
return null;
}
} });
}
}
}
Download Source Code: AndroidInputFilter






0 comments:
Post a Comment