我想爲我的應用程序使用自定義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