2013-12-09 2 views
18

Log을 개발 중이며 사용자 이름, 비밀번호 및 버튼이 있습니다. 버튼을 처음 클릭하면 아무 일도 일어나지 않지만 두 번째 버튼을 클릭하면 제대로 작동합니다. 나는 혼란 스럽다. 왜 그런 일이 일어나는가?버튼이 첫 번째 클릭으로 OnClickListener를 호출하지 않습니다.

activity_login.xml

<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" 
android:background="@drawable/splash_bg" 
tools:context=".LoginActivity" > 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentRight="true" 
android:orientation="vertical" 
android:layout_margin="30dp" > 

<EditText 
    style="@style/EditText1" 
    android:id="@+id/userEditText" 
    android:layout_marginBottom="50dp" 
    android:inputType="textNoSuggestions" 
    android:singleLine="true" 
    android:hint="username" /> 
<EditText 
    style="@style/EditText1" 
    android:id="@+id/passEditText" 
    android:inputType="textPassword" 
    android:layout_marginBottom="50dp" 
    android:hint="password" /> 
<Spinner 
    android:id="@+id/locationSpinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:popupBackground="#ffffff" 
    style="@style/EditText1" 
    android:layout_marginBottom="50dp" /> 
<Button 
    style="@style/Button1" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:onClick="onLoginClick" 
    android:text="continue" 
     /> 

loginActivity.java

@SuppressLint("DefaultLocale") 
    public void onLoginClick(View view) { 
     String username = mUserEditText.getText().toString(); 
     String password = mPassEditText.getText().toString(); 
     String location = mLocationData.get(
       mLocationSpinner.getSelectedItemPosition()).toLowerCase(); 
     if (username.isEmpty() || password.isEmpty()) { 
      CreatorMessenger 
        .getInstance() 
        .showMessage(this, "Error!!", 
          "You need to enter username and password both to continue!!"); 
      return; 
     } 
     User user; 
     user = new User(username);/* 
          * } 
           */ 
     user.setLocation(location); 
     AppManager.getInstance().setLoggedInUser(user); 

     APICaller.getInstance().login(username, password, location); 
    } 
+0

logger를 사용하여 메소드에 들어갈 지 여부를 logcat에 표시합니다. 당신은 그것이 focusableInTouchMode 매개 변수를 beacuse 아니라는 것을 볼 수 있습니다. – Jorgeblom

+0

그래서 어떻게해야합니까, 제발 도와주세요 ?? –

+0

어쩌면이 비슷한 질문에 대한 내 대답은 도움이 될 수 있습니다 : http://stackoverflow.com/a/42092582/1617737 –

답변

9

참조, 난 그냥 setOnClickListener()를 구현해야하고, setOnFocusChangeListener(). 그래서 저는 여기서 해결책을 제시 할 것입니다.

ActivityLogin.java

buttonLogin= (Button) findViewById(R.id.buttonLogin); 
    buttonLogin.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d("hello", "hellow"); 
      String username = mUserEditText.getText().toString(); 
      String password = mPassEditText.getText().toString(); 
      String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase(); 
      if(username.isEmpty()||password.isEmpty()){ 
      popbox(); 
       return; 
      } 
      User user; 
      user = new User(username); 
      user.setLocation(location); 
      AppManager.getInstance().setLoggedInUser(user); 
      APICaller.getInstance().login(username, password, location); 
     } 
    }); 
    buttonLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      v.performClick(); 
     } 
    } 
}); 
} 

activity_login.xml

<Button 
     android:id="@+id/buttonLogin" 
     style="@style/Button1" 

     android:text="continue" 
     /> 
+1

그래서 기본적으로 OnFocus() 메소드에서 onClick()을 호출합니다. 포커스를 무효로하면 효과가 있었지만 효과적이었습니다. –

+0

사실 내 문제는 잘 해결됩니다. 단추를 클릭하면 editText보기 onFocusChange 이벤트가 발생하고 첫 번째 클릭에서 발생하는 단추 클릭 이벤트가 필요하도록 단추를 포커스 할 수 있어야했습니다. 내가 focusability를 제거하면 edittext는 포커스를 잃지 않고 이벤트는 실행되지 않으며 버튼에 포커스 변경 리스너가 없으면 두 번 눌러야합니다. 그래서, 고마워. :) – Tomislav3008

25

당신은 android:focusableInTouchMode="true"를 설정하는따라서 첫 번째 클릭에서 포커스를받습니다.

예 예 내가 RND를 많이 후, 나는 해결책을 가지고, 답을 얻었다을 Android button - How to set focusable to true and still accept onClick listener on first click?

+0

확인해 보겠습니다 ... –

+0

android : focusableInTouchMode = "true"를 제거했지만 여전히 올바르게 작동하지 않습니다. –

+0

Did 프로젝트를 재건 한 다음 배포합니까? –

11

이 설정 -

android:focusableInTouchMode="true" 

to this -

android:focusableInTouchMode="false" 

버튼이 있습니다.

설명 - 버튼을 포커스 할 수있게 만들면 첫 번째 클릭시 포커스가 버튼으로 전달 된 다음 클릭이 두 번째 터치로 전달됩니다. EditText는 포커스를 먼저 얻는 포커스 가능한 뷰이므로 버튼과 마찬가지로 포커스가 작동하지 않는 한 다른 뷰가 먼저 EditText에서 포커스를 다시 가져와야합니다. OnClick 함수가 필요한 경우에는 포커스가 필요하지 않으므로 추가 클릭 한 번을 생성 할 수 있습니다.

추신 : 요구하지 않아야하지만, android:focusable을 false로 설정하면 첫 번째 효과가 작동하지 않는 경우에도 도움이됩니다. 당신의 버튼에서 XML 또는 IMG, 또는 라이너 레이아웃

2

경우가 있습니다

android:focusableInTouchMode="true" 

은 이 제거 해결하려면.

1

"onCreate()"메서드에서 요청 포커스를 사용하도록 제안했습니다.

android:focusableInTouchMode="true"을 사용하는 경우 'EditText'필드를 사용하여 포커스를 catch하지 않을 수 있습니다.

예 : 1. 액티비티에는보기 -> android : focusableInTouchMode 이 포함되어 있습니다. 2. 조각 대화 상자를 열었지만 뒤로 단추는이를 실행하기 위해 두 번 클릭해야합니다. 3. Fragment Dialog onCreateView() 후 requestFocus()를 호출해야합니다.

관련 문제