我想为我的应用程序使用自定义ViewGroup。在这我使用以下xml在我的ViewGroup类中充气。Textwatcher无法在KitKat上输入数字作为inputType
<?xml version="1.0" encoding="utf-8"?>
<!-- This layout is used wherever the pin entering screens used -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
style="?attr/txtNormalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="1"
android:inputType="numberPassword" />
<TextView
android:id="@+id/textView2"
style="?attr/txtNormalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="1"
android:inputType="numberPassword" />
<TextView
android:id="@+id/textView3"
style="?attr/txtNormalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="1"
android:inputType="numberPassword" />
<TextView
android:id="@+id/textView4"
style="?attr/txtNormalStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="1"
android:inputType="numberPassword" />
<EditText
android:id="@+id/edtInvisible"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLength="4"
android:visibility="visible" />
</LinearLayout>
而以下是我的扩展视图组类
public class View_Pin_Text extends LinearLayout implements
View.OnClickListener, TextWatcher, View.OnKeyListener {
private String strPin;
private TextView txtView1, txtView2, txtView3, txtView4;
private EditText edtText;
private boolean isInTextWatcher = false;
public View_Pin_Text(Context context, AttributeSet attrs) {
super(context, attrs);
// inflating the custom layout for the view group
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.view_pin_enter, this, true);
// 4 text views for showing pin to user
txtView1 = (TextView) findViewById(R.id.textView1);
txtView2 = (TextView) findViewById(R.id.textView2);
txtView3 = (TextView) findViewById(R.id.textView3);
txtView4 = (TextView) findViewById(R.id.textView4);
// setting on click listener
txtView1.setOnClickListener(this);
txtView2.setOnClickListener(this);
txtView3.setOnClickListener(this);
txtView4.setOnClickListener(this);
// invisible edit text for invoking keyboard
edtText = (EditText) findViewById(R.id.edtInvisible);
// text change listener to update the input in text views
edtText.addTextChangedListener(this);
// key listener to handle backspace/del keys press
edtText.setOnKeyListener(this);
}
/**
* @return strPin
* <p>
* Gives the currently given pin by the user
* </p>
*/
public String getStrPin() {
return strPin;
}
/**
* @param strPin
* <p>
* Sets the pin to instance object and updates the proper
* characters in all text views
* </p>
*/
public void setStrPin(String strPin) {
if (strPin != null) {
int lenght = strPin.length();
if (lenght <= 4)
this.strPin = strPin;
Log.d("text", strPin);
switch (lenght) {
case 0:
txtView1.setText("");
txtView2.setText("");
txtView3.setText("");
txtView4.setText("");
break;
case 1:
txtView1.setText(String.valueOf(strPin.charAt(0)));
txtView2.setText("");
txtView3.setText("");
txtView4.setText("");
break;
case 2:
txtView1.setText(String.valueOf(strPin.charAt(0)));
txtView2.setText(String.valueOf(strPin.charAt(1)));
txtView3.setText("");
txtView4.setText("");
break;
case 3:
txtView1.setText(String.valueOf(strPin.charAt(0)));
txtView2.setText(String.valueOf(strPin.charAt(1)));
txtView3.setText(String.valueOf(strPin.charAt(2)));
txtView4.setText("");
break;
case 4:
txtView1.setText(String.valueOf(strPin.charAt(0)));
txtView2.setText(String.valueOf(strPin.charAt(1)));
txtView3.setText(String.valueOf(strPin.charAt(2)));
txtView4.setText(String.valueOf(strPin.charAt(3)));
((InputMethodManager) getContext().getSystemService(
Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
edtText.getWindowToken(), 0);
break;
}
} else {
this.strPin = strPin;
}
}
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "OnClick", Toast.LENGTH_SHORT).show();
((InputMethodManager) getContext().getSystemService(
Context.INPUT_METHOD_SERVICE)).showSoftInput(edtText,
InputMethodManager.SHOW_FORCED);
}
@Override
public void afterTextChanged(Editable s) {
if (isInTextWatcher)
return;
isInTextWatcher = true;
Log.d("text", "changed-" + s.toString());
if (getStrPin() == null) {
setStrPin(s.toString());
} else {
setStrPin(getStrPin() + "" + s.toString());
}
edtText.setText("");
isInTextWatcher = false;
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL
&& event.getAction() != KeyEvent.ACTION_DOWN) {
if (getStrPin() != null) {
int length = getStrPin().length();
if (length > 0) {
setStrPin(getStrPin()
.substring(0, getStrPin().length() - 1));
}
}
}
return true;
}
}
在此上课的时候我的编辑文本设置为安卓afterTextChanged我的文字观察家则不会触发:的inputType =“号”和适用于我的nexus 5(Kitkat 4.4.4)中的文本输入。但是,当我用三星核心(Jellybean 4.1.2)尝试它的工作正常..那么,这是什么问题,这应该如何解决?
你有没有想过这个?我仍然有这个问题。我在我的TextWatcher中将“/”添加到出生日期,并且我只需要数字,但现在不会使用数字来触发 – 2014-12-01 22:14:35