2013-12-09 17 views
18

Tôi đang cố gắng phát triển một số Log trong trang, nơi tôi có Tên người dùng, mật khẩu và nút. Khi tôi nhấp vào nút lần đầu tiên, không có gì xảy ra, nhưng khi tôi nhấp vào nút lần thứ hai, thì nó hoạt động bình thường. Tôi bối rối, tại sao nó lại xảy ra?Nút không gọi OnClickListener với lần nhấp đầu tiên

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

Sử dụng Trình ghi nhật ký để xem trong logcat nếu nó đang nhập phương thức hay không. Bạn sẽ thấy nó không phải là beacuse paramableInTouchMode param. – Jorgeblom

+0

SO tôi nên làm gì, hãy giúp tôi? –

+0

Có lẽ câu trả lời của tôi cho câu hỏi tương tự này có thể giúp: http://stackoverflow.com/a/42092582/1617737 –

Trả lời

9

Có Có, tôi có câu trả lời, sau khi rất nhiều RND, tôi có những giải pháp, tôi chỉ cần phải thực hiện setOnClickListener(), và setOnFocusChangeListener(). Vì vậy, tôi đặt ở đây giải pháp.

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

Vì vậy, về cơ bản bạn đang gọi onClick() trên phương thức OnFocus(). Vô hiệu hóa tập trung nên đã làm việc như nhau nhưng vui mừng này làm việc ra. –

+0

Thực tế hoạt động tốt cho vấn đề của tôi. Tôi cần các nút để được tập trung để xem editText của tôi onFocusChange sự kiện bị sa thải khi tôi nhấp vào nút, và tôi cũng cần sự kiện bấm nút sẽ được kích hoạt trên nhấp chuột đầu tiên. Nếu tôi loại bỏ tiêu điểm của nó, edittext không mất tiêu điểm của nó và sự kiện của nó không cháy, và không có người nghe thay đổi tập trung vào nút nó phải được nhấn hai lần. cảm ơn :) – Tomislav3008

11

Set này -

android:focusableInTouchMode="true" 

cho điều này -

android:focusableInTouchMode="false" 

trên nút.

Giải thích - Nếu bạn sẽ làm cho nút có thể lấy nét khi nhấp vào đầu tiên, tiêu điểm được chuyển đến nút, sau đó nhấp chuột được chuyển vào lần chạm thứ hai. EditText là Chế độ xem có thể lấy tiêu điểm để lấy nét đầu tiên và do đó các chế độ xem khác trước tiên phải lấy lại tiêu điểm từ EditText, trừ khi chúng không cần tiêu điểm để hoạt động, giống như các nút. Nếu bạn chỉ cần chức năng OnClick, thì bạn không cần tập trung, vì vậy bạn có thể tăng thêm một cú nhấp chuột.

PS: Mặc dù không cần thiết, nhưng việc đặt android:focusable thành false cũng sẽ giúp ích nếu trường hợp đầu tiên không hoạt động.

2

nếu trong xml của bạn trong nút, hoặc img, hoặc bố trí liner có:

android:focusableInTouchMode="true" 

Để giải quyết, chỉ cần loại bỏ điều này.

1

Tôi đề xuất sử dụng tiêu điểm yêu cầu trong phương thức "onCreate()".

Khi bạn sử dụng android:focusableInTouchMode="true" bạn có thể không muốn lấy nét lấy nét bằng trường 'EditText'.

Ví dụ: 1. Hoạt động có chế độ xem chứa -> android: focusableInTouchMode 2. Bạn mở Hộp thoại phân đoạn, nhưng nút quay lại cần nhấp hai lần để kích hoạt. 3. Bạn nên gọi requestFocus() sau Hộp thoại Fragment onCreateView()

Các vấn đề liên quan