사용자 정의 핀 패드 (화면의 버튼)를 사용하여 4 자리 PIN 번호를 받아 들일 필요가있는 간단한 활동이 있습니다. 숫자는 네 개의 편집 텍스트에 저장됩니다. I가 버튼을 클릭하면myEditText.setText 페이지가 새로 고침되지 않습니다.
버튼의 텍스트가 char[]
에 저장된 포커스가 onFocusListener()
통해 다음 EditText
로 이동된다 (myEditText.getText()
이 일어나고 있음을 나타낸다)와 Log
출력이 모두 일어나는 것을 알 바르게.
11-20 10:19:56.969: I/DebugA(17742): Pin1 updated to: 1 // Pin1 is the ID 11-20 10:19:58.289: I/DebugA(17742): Pin2 updated to: 2 // '2' is Pin2.getText() 11-20 10:19:58.849: I/DebugA(17742): Pin3 updated to: 3 11-20 10:19:59.659: I/DebugA(17742): Pin4 updated to: 4
화면 자체가 단순히 업데이트되지 않습니다. EditTexts는 코드가 완벽하게 실행 되더라도 모두 비어있는 것처럼 보입니다.
나는 SO에 대한 답변 전체를 살펴본 결과 절대적으로 운이없는 제안을 많이 시도했기 때문에이 질문을 게시하여 누군가가 밝힐 수 있기를 바랍니다. 다른 사람이 이런 일이 있었나요?
<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" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" >
<EditText
android:id="@+id/Pin1"
android:tag="Pin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="2"
android:gravity="center_vertical|center_horizontal"
android:nextFocusDown="@+id/Pin2"
android:maxLength="1"
android:inputType="textPassword" />
...
...
...
</RelativeLayout>
을 ... 그리고 버튼은 다음과 같습니다 :
다음은 레이아웃 파일의 일부이다
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:layout_below="@+id/layout1" >
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:text="1" />
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="2" />
<Button
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="3" />
</LinearLayout>
활동은 표준 활동 (MyActivity extends Activity
)입니다.
의견이 있으십니까?
UPDATE
요청 여기 자바의 일부이다 :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.activity_login, null);
EditText Pin1 = (EditText) layout.findViewById(R.id.Pin1);
Pin1.setOnFocusChangeListener(focusListener);
Pin1.setText("1"); // <--THIS HAS NO EFFECT
EditText Pin2 = (EditText) layout.findViewById(R.id.Pin2);
Pin2.setOnFocusChangeListener(focusListener);
EditText Pin3 = (EditText) layout.findViewById(R.id.Pin3);
Pin3.setOnFocusChangeListener(focusListener);
EditText Pin4 = (EditText) layout.findViewById(R.id.Pin4);
Pin4.setOnFocusChangeListener(focusListener);
...
...
...
}
Onclick
방법은 :
public void btnPinClick(View btnPin) {
String PinNumber = ((Button)btnPin).getText().toString();
RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_login, null);
ArrayList<View> views = (ArrayList<View>) layout.getFocusables(View.FOCUS_DOWN);
EditText nextItem = null;
for (int i = 0; i < views.size(); i++) {
// THIS IS THE CORRECT EditText FOR EACH OF THESE
EditText textBox = (EditText) views.get(i);
if (textBox.getTag().toString().equals(currentText)) {
textBox.setText(PinNumber);
pin[i] = PinNumber.toCharArray()[0];
Log.i("DebugA", textBox.getTag().toString() + " updated to: " + textBox.getText().toString());
textBox.invalidate();
textBox.refreshDrawableState();
if(i+1 < views.size()) {
nextItem = (EditText) views.get(i+1);
}
else {
doCheckPin();
}
break;
}
}
if(nextItem != null){
nextItem.requestFocus();
currentText = nextItem.getTag().toString();
}
}
UPDATE 여기서 2
로 OnFocusChangeListener위한 코드 요청 :
protected OnFocusChangeListener focusListener = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//THIS SIMPLY SETS A VARIABLE STRING TO SAY WHICH
//EditText HAS FOCUS.
currentText = v.getTag().toString();
}
}
};
필자의'OnCreate' ovveride에'Pin1.setText()'를 호출해도 아무 효과가 없다고 덧붙여 야합니다. – CodeMonkey
무슨 일이 일어나는지 알 수 있도록 관련 Java 코드를 게시하십시오. 실수로 EditText를 다른 것으로 덮고 있습니까? – Sam
Thanks @Sam, 질문이 업데이트되었습니다. 'onclick'과'OnCreate' 두 가지 모두 레이아웃에서 올바른 요소들을 끌어 당기는 것 같습니다 ... – CodeMonkey