2014-03-27 1 views
1

키 이벤트를 내 EditText's에 수신하고 next을 누를 때마다 EditText의 텍스트를 내 데이터 개체에 저장하려고합니다. 물론Android onKey()가 호출되지 않습니다.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
    savedInstanceState) { 
    v = inflater.inflate(R.layout.frag_userprofile_myinfo, container, false); 
    context = getActivity().getApplicationContext(); 
    global = (eKeshGlobal) context; 

    pbWait = (ProgressBar) v.findViewById(R.id.pbWait); 

    bnDone = (Button) v.findViewById(R.id.bnDone); 
    rlBack = (RelativeLayout) v.findViewById(R.id.rlBack); 

    etFirstName = (EditText) v.findViewById(R.id.etFirstName); 
    etLastName = (EditText) v.findViewById(R.id.etLastName); 
    etAdress = (EditText) v.findViewById(R.id.etAdress); 
    etMobile = (EditText) v.findViewById(R.id.etMobile); 
    etDob = (EditText) v.findViewById(R.id.etDob); 
    etOib = (EditText) v.findViewById(R.id.etOib); 

    etFirstName.setOnKeyListener(this); 
    etLastName.setOnKeyListener(this); 
    etAdress.setOnKeyListener(this); 
    etMobile.setOnKeyListener(this); 
    etDob.setOnKeyListener(this); 
    etOib.setOnKeyListener(this); 


    return v; 

} 

@Override 
public boolean onKey(View v, int keyCode, KeyEvent event) { 
    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == 
KeyEvent.KEYCODE_ENTER)) { 

     switch(v.getId()) { 

     case R.id.etFirstName: 
      user.setFirstName(etFirstName.getText().toString()); 
      return true; 

     case R.id.etLastName: 
      user.setLastName(etLastName.getText().toString()); 
      return true; 

     case R.id.etAdress: 
      user.setAddress(etAdress.getText().toString()); 
      return true; 

     case R.id.etMobile: 
      user.setMobile(etMobile.getText().toString()); 
      return true; 

     case R.id.etDob: 
      user.setDateOfBirth(Utils.formatDate(context, 
etDob.getText().toString())); 
      return true; 

     case R.id.etOib: 
      user.setOib(etOib.getText().toString()); 
      return true; 

     default: 
      return false; 
     } 
    } 

    return false; 
} 

내가 구현 한 청취자 : 내가 호출되지 왜 그럴수 OnKeyListener하고 onKey() 방법을 구현 ... 여기 내 코드의

public class UserProfileMyInfo extends Fragment implements View.OnKeyListener

그리고 여기 샘플입니다 EditText의 XML : 난 정말이 작품을 얻기 위해 아이디어의 부족 해요

<EditText 
     android:id="@+id/etLastName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:ems="10" 
     android:hint="@string/my_profile_lastname" 
     android:imeOptions="actionNext" 
     android:inputType="text" 
     android:singleLine="true" /> 

, 내가 함께 tryed onKeyDown()도 작동하지 않았다. .. 미리 감사드립니다!

+0

내가 왜 안 보이는지 모르겠다. 작동하지 않는다고 생각하는 또 다른 이유가 없다고 확신 할 수 있습니까? onKey의 첫 번째 줄에 중단 점을 넣으면 충돌이 발생합니까? – Eluvatar

답변

1

다음 키 이벤트 만 캡처하려면 android:imeOptions을 사용해야합니다. 같은 매니페스트에

설정이,

<EditText 
android:id="@+id/search" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:hint="@string/search_hint" 
android:inputType="text" 
android:imeOptions="actionNext" /> 

과는 다른 옵션의 전체 무리가있다

EditText editText = (EditText) findViewById(R.id.search); 
editText.setOnEditorActionListener(new OnEditorActionListener() { 
@Override 
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
    boolean handled = false; 
    if (actionId == EditorInfo.IME_ACTION_SEND) { 
     //do your stuff here 
     handled = true; 
    } 
    return handled; 
} 
}); 

당신의 EdiText에 setOnEditorActionListener() 메서드를 호출 코드에서이 문제를 잡을 수있어. 방문하십시오 here.

+0

완벽하게 작동합니다. 감사합니다! – pavle

2

문서에서 View.OnKeyListener은 하드웨어 키용입니다. 이것이 작동하지 않는 이유입니다.

하드웨어 키 이벤트가이보기에 전달 될 때 호출 될 콜백의 인터페이스 정의입니다. 키 이벤트가 전에 콜백이 호출됩니다. 이것은 하드웨어에만 유용합니다 키보드; 소프트웨어 입력 방법은이 청취자를 트리거 할 의무가 없습니다.

+1

유용한 정보를 주셔서 감사합니다. [SoftKey, Detect Done] (http://stackoverflow.com/a/5077543/5212904),'View.OnKeyListener'는 하드웨어 키를위한 것이기 때문에 가능한 해결책을 추가하겠습니다. – ArtiomLK

관련 문제