2014-11-19 23 views
18

tôi đang cố gắng để có được một màn hình đăng nhập cho một ứng dụng Android và cho đến nay đây là mã của tôi:imeOptions Android = "actionDone" không làm việc

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 


    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/username" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Username" 
      android:inputType="text" 
      android:singleLine="true" 
      android:imeOptions="actionNext"> 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/password" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Password" 
      android:inputType="textPassword" 
      android:singleLine="true" 
      android:imeOptions="actionDone" /> 

     <Button 
      android:id="@+id/buttonLaunchTriage" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:text="@string/login" /> 
    </LinearLayout> 



</RelativeLayout> 

Khi tôi cố gắng chạy nó, bàn phím cho thấy bên phải nhưng khi tôi cố gắng nhấn xong sau khi nhập mật khẩu, không có gì xảy ra. Tôi đang sử dụng tính năng này để xử lý nút nhấn:

private void setupLoginButton() { 
    Button launchButton = (Button) findViewById(R.id.buttonLaunchTriage); 
    launchButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      EditText username = (EditText) findViewById(R.id.patient_start_userName_value); 
      EditText password = (EditText) findViewById(R.id.patient_start_password_value); 

      try { 
       if(TriageApplicationMain.validateUser(username.getText().toString(),password.getText().toString(),getApplicationContext())) 
       { 
       Toast.makeText(StartActivity.this, 
         "Launching Triage Application", Toast.LENGTH_SHORT) 
         .show(); 
       startActivity(new Intent(StartActivity.this, MainActivity.class)); 
       } 

       else 
        { 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
          StartActivity.this); 


         // set dialog message 
         alertDialogBuilder 
          .setMessage("Incorrect Credentials") 
          .setCancelable(false) 
          .setPositiveButton("OK",new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog,int id) { 
            // if this button is clicked, close 
            // current activity 
           dialog.cancel();  
           } 
           }); 


          // create alert dialog 
          AlertDialog alertDialog = alertDialogBuilder.create(); 

          // show it 
          alertDialog.show(); 

        } 
       } 


      catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

} 

Tôi biết điều này rất nhiều mã, nhưng nếu có ai có thể giúp tôi ở đây thì sẽ rất tuyệt. Đây là cho một dự án trường học.

PS: Tôi đã tìm kiếm thông qua Google trong một giờ vững chắc trước khi đăng bài này vì vậy xin đừng chỉ trích vì đã không làm điều đó. Nếu bạn tìm thấy một liên kết hữu ích thì hãy chia sẻ.

+0

gì chính xác là bạn mong đợi từ "làm" trong bàn phím mềm đầu vào? tự ẩn bàn phím hoặc tiến hành đăng nhập? – waqaslam

+0

tôi đã gặp phải vấn đề tương tự, bạn sẽ phải thêm: android: imeActionLabel = "Xong" android: singleLine = "true" đây là câu trả lời http://stackoverflow.com/questions/5578991/actiondone -imeoption-doesnt-work-on-edittext-in-android-2-3/5579944 # 5579944 –

Trả lời

29

Bạn nên đặt OnEditorActionListener cho EditText để triển khai tác vụ bạn muốn thực hiện khi người dùng nhấp vào "Xong" trên bàn phím.

Vì vậy, bạn cần phải viết một số mã như:

password.setOnEditorActionListener(new OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if (actionId == EditorInfo.IME_ACTION_DONE) { 
      // Do whatever you want here 
      return true; 
     } 
     return false; 
    } 
}); 

Xem hướng dẫn tại android developer site

+0

cảm ơn câu trả lời. Tôi loại bỏ từ stackoverflow trong một thời gian.trở lại ngay bây giờ :) – Mak

19

Qianqian là đúng. Mã của bạn chỉ lắng nghe sự kiện bấm nút, không phải cho sự kiện EditorAction.

Tôi muốn thêm rằng một số nhà cung cấp điện thoại có thể không triển khai đúng cách tác vụ DONE. Tôi đã thử nghiệm điều này với một A889 Lenovo ví dụ, và điện thoại mà không bao giờ gửi EditorInfo.IME_ACTION_DONE khi bạn nhấn thực hiện, nó luôn luôn gửi EditorInfo.IME_ACTION_UNSPECIFIED vì vậy tôi thực sự kết thúc với một cái gì đó giống như

myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) 
    { 
    boolean handled=false; 

    // Some phones disregard the IME setting option in the xml, instead 
    // they send IME_ACTION_UNSPECIFIED so we need to catch that 
    if(EditorInfo.IME_ACTION_DONE==actionId || EditorInfo.IME_ACTION_UNSPECIFIED==actionId) 
    { 
     // do things here 

     handled=true; 
    } 

    return handled; 
    } 
}); 

Cũng lưu ý "xử lý" cờ (Qianqian không giải thích phần đó). Nó có thể là các OnEditorActionListeners khác cao hơn đang lắng nghe các sự kiện của một kiểu khác. Nếu phương thức của bạn trả về false, điều đó có nghĩa là bạn đã không xử lý sự kiện này và nó sẽ được chuyển cho người khác. Nếu bạn trả lại đúng nghĩa là bạn đã xử lý/tiêu thụ nó và nó sẽ không được chuyển cho người khác.

+2

Cảm ơn tôi đã chính xác nhận được IME_ACTION_UNSPECIFIED này thay vì IME_ACTION_SEND – flaviodesousa

+1

Điều này sẽ là câu trả lời đúng – HendraWD

24

Chỉ cần thêm android:inputType="..." vào EditText của bạn. Nó sẽ hoạt động !! :)

4

Sử dụng android:inputType="Yours" sau đó

android:lines="1" 
android:imeOptions="actionDone" 
0

Bạn nên đặt OnEditorActionListener cho EditText để thực hiện các hành động mà bạn muốn thực hiện khi người dùng nhấp chuột vào "Done" trong bàn phím.

0

Sau khi thử nhiều thứ, đây là những gì làm việc cho tôi:

android:maxLines="1" 
    android:inputType="text" 
    android:imeOptions="actionDone" 
+0

Điều này không cung cấp câu trả lời cho câu hỏi. Khi bạn có đủ [danh tiếng] (https://stackoverflow.com/help/whats-reputation), bạn sẽ có thể [nhận xét về bất kỳ bài đăng nào] (https://stackoverflow.com/help/privileges/comment); thay vào đó, [cung cấp câu trả lời không yêu cầu làm rõ từ người hỏi] (https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- i-do-thay thế). - [Từ đánh giá] (/ đánh giá/bài đăng chất lượng thấp/18007334) –

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