2012-11-19 3 views
1

사용자 정의 핀 패드 (화면의 버튼)를 사용하여 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(); 
     } 

    } 
}; 
+0

필자의'OnCreate' ovveride에'Pin1.setText()'를 호출해도 아무 효과가 없다고 덧붙여 야합니다. – CodeMonkey

+0

무슨 일이 일어나는지 알 수 있도록 관련 Java 코드를 게시하십시오. 실수로 EditText를 다른 것으로 덮고 있습니까? – Sam

+0

Thanks @Sam, 질문이 업데이트되었습니다. 'onclick'과'OnCreate' 두 가지 모두 레이아웃에서 올바른 요소들을 끌어 당기는 것 같습니다 ... – CodeMonkey

답변

1

귀하의 활동에 setContentView 외에도 레이아웃 인플레이터를 사용하고있는 것 같습니다. 그리고 inflater에 의해 참조 된 edittexts에 영향을 미칩니다. 인플레이터의 사용을 제거하고이 액티비티 부분을 위해 setContentView에만 의존하십시오. 잘못된 뷰 계층에서 수행 중일 수 있습니다.

+0

감사합니다 망고. 나는 지난 주 동안 Fragments와 함께 일해 왔으며 그 다음 바로 위로 활동으로 전환했습니다. 정말로이 하나에 눈의 신선한 세트가 필요했습니다! – CodeMonkey

관련 문제