10

Xin chào, tôi cần bàn phím mềm chỉ với các giá trị số đến và Nhập khóa. Không nên hiển thị bất kỳ thông tin nào khác ngoài các loại này như .,() vv ...Chỉ cần số bàn phím mềm?

enter image description here

tôi đã cố gắng một vài lựa chọn as suggested here nhưng nothings dường như làm việc cho tôi.

  1. setRawInputType(Configuration.KEYBOARD_QWERTY)
  2. setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED)
  3. setRawInputType(InputType.TYPE_CLASS_NUMBER)
  4. setRawInputType(InputType.TYPE_CLASS_PHONE)

tôi luôn luôn có các nhân vật phụ xuất hiện trên bàn phím như:

enter image description here

setRawInputType(Configuration.KEYBOARD_12KEY) cho thấy một bàn phím như thế này:

enter image description here

Sẽ đánh giá cao sự giúp đỡ nào. Cảm ơn trước.

LƯU Ý:

  • android:minSdkVersion="14": ICS4.0
  • android:targetSdkVersion="17": JB 4,2
+0

setRawInputType (Configuration.KEYBOARD_12KEY) cung cấp cho bạn những gì? – yarian

+0

Bài đăng được cập nhật với ảnh chụp màn hình. – AKh

Trả lời

4

Tất cả những gì bạn có thể làm cho bàn phím tiêu chuẩn là đề xuất loại đầu vào. Bàn phím vẫn có thể hiển thị hoặc không hiển thị bất kỳ phím nào mà nó muốn. Nếu bạn phải có các khóa nhất định và chỉ những khóa đó, bạn cần phải tạo bàn phím mềm tùy chỉnh. Nếu nó chỉ dành cho ứng dụng của bạn, và đặc biệt nếu nó chỉ cho một hoạt động, tôi sẽ không thực sự thực hiện một bàn phím tiêu chuẩn, nhưng chỉ sử dụng các khung nhìn/nút thực hiện các hành động thích hợp.

0

Bàn phím tự chọn phím gì để layout. Điều tốt nhất bạn có thể làm là chỉ định InputType.TYPE_CLASS_NUMBER, nhưng bàn phím sẽ vẫn hiển thị bất cứ điều gì nó cho là phù hợp với trường văn bản số.

+0

cảm ơn bạn.Tôi có thể đề cập đến loại đầu vào có thể được mong đợi trong văn bản chỉnh sửa trong chương trình không? – AKh

+0

Màn hình khóa trên tất cả các thiết bị dường như chỉ có bàn phím này. Bất kỳ ý tưởng nếu đây là bàn phím hệ thống hoặc tùy chỉnh được xây dựng? – AKh

+2

Chúng được xây dựng tùy chỉnh. Bạn luôn có thể viết bàn phím tùy chỉnh của riêng bạn, nhưng nó không thực sự đáng giá nhiều thời gian. Đối với một số bạn thậm chí có thể tốt hơn không làm cho nó một bàn phím và hành động như thể nó là một ứng dụng máy tính khổng lồ. Tôi nghĩ rằng đó là những gì họ làm- 9 nút trong bố cục cho bàn phím và chèn văn bản thủ công vào nút onClick của nút. –

1

Ngoài ra, thiết lập inputType = "phone" trên EditText. Điều đó sẽ mở bàn phím số pad một khi bạn bắt đầu gõ tuy nhiên nó sẽ bao gồm tất cả các ký tự phụ liên quan đến các con số. Bạn sẽ cần phải thực hiện bàn phím của riêng bạn để chỉ giữ các giá trị số.

+0

Tôi đã thử inputType = "phone" và như bạn đã đề cập, nó vẫn hiển thị các ký tự thừa đó. Tôi nghĩ rằng bàn phím tùy chỉnh là con đường để đi. Cảm ơn – AKh

0

tôi đã có cùng một vấn đề mà bạn có, và chỉ cần đi kèm với một giải pháp, có lẽ nó không thanh lịch, cũng không phải dễ dàng của nó, nhưng nó không làm việc lộng lẫy ...

Trước hết, các InputType duy nhất mà làm việc với bàn phím đó (ít nhất là đến 4.3) là "numberPassword", nhưng điều này "ẩn" đầu vào của bạn dưới dạng dấu chấm.vì vậy tôi sử dụng mà đầu vào với phương pháp chuyển đổi này:

private class ShowNumbersTransformationMethod implements TransformationMethod { 
    public CharSequence getTransformation(final CharSequence charSequence, final View view) { 
     return new PassCharSequence(charSequence); 
    } 

    @Override 
    public void onFocusChanged(final View view, final CharSequence charSequence, final boolean b, final int i, 
      final Rect rect) { 
     //nothing to do here 
    } 

    private class PassCharSequence implements CharSequence { 

     private final CharSequence charSequence; 

     public PassCharSequence(final CharSequence charSequence) { 
      this.charSequence = charSequence; 
     } 

     @Override 
     public char charAt(final int index) { 
      return charSequence.charAt(index); 
     } 

     @Override 
     public int length() { 
      return charSequence.length(); 
     } 

     @Override 
     public CharSequence subSequence(final int start, final int end) { 
      return new PassCharSequence(charSequence.subSequence(start, end)); 
     } 
    } 
} 

và sau đó đặt nó vào EditText của bạn:

edittext.setTransformationMethod(new ShowNumbersTransformationMethod()); 

Bây giờ, như đã nói trước, đây không phải là giải pháp hạnh phúc nhất, nhưng tôi đảm bảo với bạn nó hoạt động như một sự quyến rũ. Việc tạo bàn phím tùy chỉnh của riêng bạn sẽ dễ dàng hơn 10 lần, nhưng tôi không có tùy chọn đó, vì khách hàng của tôi muốn có bàn phím chuẩn, hãy hiểu tại sao ...

Hy vọng điều đó đã hữu ích!

+0

Số cài đặtPassword sẽ khiến bạn mất phông chữ tùy chỉnh cho thành phần này. –

4

Tôi đã gặp phải vấn đề tương tự, và tôi thấy không có bàn phím android như thế này có sẵn và cách duy nhất là thực hiện của riêng bạn. vì vậy tôi muốn chia sẻ với bạn my thực hiện và hy vọng bạn tiết kiệm một số thời gian quý báu:

  1. tôi đã tạo xml này, bạn có thể thay đổi màu sắc, phông chữ và kích thước của bàn phím accourding yêu cầu của bạn :

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" > 
    
    <LinearLayout 
        android:id="@+id/one_to_three" 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:layout_alignParentTop="true" 
        android:layout_centerHorizontal="true" 
        android:orientation="horizontal" 
        android:weightSum="3" > 
    
        <Button 
         android:id="@+id/one_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="1" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/two_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="2" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/three_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="3" 
         android:textSize="25sp" /> 
    </LinearLayout> 
    
    <LinearLayout 
        android:id="@+id/four_to_six" 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:layout_below="@+id/one_to_three" 
        android:orientation="horizontal" 
        android:weightSum="3" > 
    
        <Button 
         android:id="@+id/four_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="4" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/five_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="5" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/six_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="6" 
         android:textSize="25sp" /> 
    </LinearLayout> 
    
    <LinearLayout 
        android:id="@+id/seven_to_nine" 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:layout_below="@+id/four_to_six" 
        android:orientation="horizontal" 
        android:weightSum="3" > 
    
        <Button 
         android:id="@+id/seven_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="7" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/eight_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="8" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/nine_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="9" 
         android:textSize="25sp" /> 
    </LinearLayout> 
    
    <LinearLayout 
        android:id="@+id/zero" 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:layout_below="@+id/seven_to_nine" 
        android:orientation="horizontal" 
        android:weightSum="3" > 
    
        <Button 
         android:id="@+id/zero_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="2" 
         android:text="0" 
         android:textSize="25sp" /> 
    
        <Button 
         android:id="@+id/back_btn" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_weight="1" 
         android:text="Back" 
         android:textSize="25sp" /> 
    </LinearLayout> 
    
    <LinearLayout 
        android:id="@+id/done" 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:layout_below="@+id/zero" 
        android:orientation="horizontal" > 
    
        <Button 
         android:id="@+id/done_btn" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="Done" 
         android:textSize="30sp" /> 
        </LinearLayout> 
        </RelativeLayout> 
    

enter image description here

  1. tôi đã tạo đoạn này:

     package com.galrom.keyboard; //replace it with your package 
         import com.example.calculator.R;//import your own R class 
         import android.app.Activity; 
         import android.os.Bundle; 
         import android.support.v4.app.Fragment; 
         import android.util.Log; 
         import android.view.LayoutInflater; 
         import android.view.View; 
         import android.view.ViewGroup; 
         import android.view.View.OnLongClickListener; 
         import android.widget.Button; 
         public class KeyBoardFragment extends Fragment { 
    
         private Button one_btn; 
         private Button two_btn; 
         private Button three_btn; 
         private Button four_btn; 
         private Button five_btn; 
         private Button six_btn; 
         private Button seven_btn; 
         private Button eight_btn; 
         private Button nine_btn; 
         private Button zero_btn; 
         private Button back_btn; 
         private Button done_btn; 
    
         private StringBuilder sb; 
    
         private onKeyBoardEvent keyboardEventListener; 
    
    
         private int maxLength=10; 
         private int currentLength; 
    
         public static KeyBoardFragment newInstance(String EditTextValue) 
         { 
          KeyBoardFragment fragment=new KeyBoardFragment(); 
          Bundle bundle=new Bundle(); 
          bundle.putString("et_value", EditTextValue); 
          fragment.setArguments(bundle); 
          return fragment; 
         } 
    
         @Override 
         public void onAttach(Activity activity) { 
          try{ 
    
           keyboardEventListener=(onKeyBoardEvent)activity; 
          } 
          catch(ClassCastException e) 
          { 
           Log.e("ClassCastException in KeyBoardFragment row 50",activity.toString()+" must implement onKeyboardEvent"); 
           e.printStackTrace(); 
          } 
    
          super.onAttach(activity); 
         } 
    
         @Override 
         public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
          // TODO Auto-generated method stub 
          sb=new StringBuilder(getArguments().getString("et_value")); 
          currentLength=sb.length(); 
          View rootView=inflater.inflate(R.layout.numeric_keyboard_layout, container, false); 
          one_btn=(Button)rootView.findViewById(R.id.one_btn); 
          one_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
            // TODO Auto-generated method stub 
            add("1"); 
           } 
          }); 
          two_btn=(Button)rootView.findViewById(R.id.two_btn); 
          two_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("2"); 
           } 
          }); 
          three_btn=(Button)rootView.findViewById(R.id.three_btn); 
          three_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("3"); 
    
           } 
          }); 
          four_btn=(Button)rootView.findViewById(R.id.four_btn); 
          four_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("4"); 
           } 
          }); 
          five_btn=(Button)rootView.findViewById(R.id.five_btn); 
          five_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("5"); 
    
           } 
          }); 
          six_btn=(Button)rootView.findViewById(R.id.six_btn); 
          six_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
    
            add("6"); 
           } 
          }); 
          seven_btn=(Button)rootView.findViewById(R.id.seven_btn); 
          seven_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("7"); 
           } 
          }); 
          eight_btn=(Button)rootView.findViewById(R.id.eight_btn); 
          eight_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("8"); 
    
           } 
          }); 
          nine_btn=(Button)rootView.findViewById(R.id.nine_btn); 
          nine_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            add("9"); 
           } 
          }); 
          zero_btn=(Button)rootView.findViewById(R.id.zero_btn); 
          zero_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            if(sb.length()>0) 
             add("0"); 
           } 
          }); 
          back_btn=(Button)rootView.findViewById(R.id.back_btn); 
          back_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
    
            if(sb.length()>0) 
            { 
             currentLength--; 
             sb.deleteCharAt((sb.length())-1); 
             keyboardEventListener.backButtonPressed(sb.toString()); 
            } 
           } 
          }); 
          back_btn.setOnLongClickListener(new View.OnLongClickListener() { 
    
           @Override 
           public boolean onLongClick(View v) { 
    
            currentLength=0; 
            sb=new StringBuilder(); 
            keyboardEventListener.backLongPressed(); 
            return false; 
           } 
          }); 
          done_btn=(Button)rootView.findViewById(R.id.done_btn); 
          done_btn.setOnClickListener(new View.OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
            keyboardEventListener.doneButtonPressed(sb.toString()); 
           } 
          }); 
          return rootView; 
         } 
         public interface onKeyBoardEvent 
         { 
          public void numberIsPressed(String total); 
          public void doneButtonPressed(String total); 
          public void backLongPressed(); 
          public void backButtonPressed(String total); 
         } 
    
         public int getMaxLength() { 
          return maxLength; 
         } 
    
         public void setMaxLength(int maxLength) { 
          this.maxLength = maxLength; 
         } 
         public void add(String num) 
         { 
          currentLength++; 
          if(currentLength<=maxLength) 
          { 
    
           sb.append(num); 
           keyboardEventListener.numberIsPressed(sb.toString()); 
          } 
          else 
           currentLength--; 
         } 
        } 
    

3.The ảnh hưởng của một bàn phím poping dưới EditText khi nó được ép được achived bởi tạo ra một RelativeLayout trống có chức năng như một container để bàn phím:

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<com.galrom.keyboard.EditTextNoKeyBoard 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Key_board_container" 
    android:layout_centerHorizontal="true" 
    android:clickable="true" 
    android:ems="10" /> 

<RelativeLayout 
    android:id="@+id/Key_board_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="38dp" 
    android:background="#ffffff" > 
</RelativeLayout> 

khi người dùng nhấn vào EditText, chúng tôi thêm phân đoạn vào vùng chứa và khi nhấn xong, chúng tôi sẽ ẩn nó. đoạn bàn phím kết hợp với Hoạt động với giao diện onKeyBoardEvent. LƯU Ý: hoạt động lưu trữ phải triển khai giao diện này hoặc nếu không một ClassCastException sẽ bị ẩn.

RẤT QUAN TRỌNG: tôi không xử lý thay đổi định hướng, nếu bạn thay đổi thành ladscape trong khi bàn phím mở, nó sẽ bị hỏng, do đó, hoặc tắt chế độ ngang hoặc xử lý thay đổi hướng để tránh nullPointerException trên key_board_fragment.

đây là Hoạt động rằng implemets bàn phím:

 package com.galrom.keyboard; 

    import com.example.calculator.R; 

    import android.content.res.Configuration; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.util.Log; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.EditText; 
    import android.widget.Toast; 

    public class MainActivity extends FragmentActivity implements    KeyBoardFragment.onKeyBoardEvent{ 

private EditText et; 
private KeyBoardFragment keyboard_fragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    et=(EditText)findViewById(R.id.editText1); 
    et.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(keyboard_fragment==null) 
      { 
       keyboard_fragment=KeyBoardFragment.newInstance(et.getText().toString()); 

        getSupportFragmentManager().beginTransaction().add(R.id.Key_board_container, keyboard_fragment).commit(); 


      } 
      else 
      { 
       if(keyboard_fragment.isVisible()) 
        getSupportFragmentManager().beginTransaction().hide(keyboard_fragment).commit(); 
       else 
       { 
        keyboard_fragment=KeyBoardFragment.newInstance(et.getText().toString()); 
        getSupportFragmentManager().beginTransaction().add(R.id.Key_board_container, keyboard_fragment).commit(); 
       } 
      } 
    }); 
} 

@Override 
public void numberIsPressed(String total) { 
    // TODO Auto-generated method stub 
    et.setText(total); 
} 

@Override 
public void doneButtonPressed(String total) { 
    // TODO Auto-generated method stub 
    et.setText(total); 
    if(keyboard_fragment.isVisible()) 
     getSupportFragmentManager().beginTransaction().hide(keyboard_fragment).commit(); 
} 

@Override 
public void backLongPressed() { 
    // TODO Auto-generated method stub 
    et.setText(""); 
} 

@Override 
public void backButtonPressed(String total) { 
    // TODO Auto-generated method stub 
    et.setText(total); 
} 

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    if(keyboard_fragment!=null) 
    { 
     if(keyboard_fragment.isVisible()) 
      getSupportFragmentManager().beginTransaction().remove(keyboard_fragment).commit(); 
     else 
      super.onBackPressed(); 
    } 
    else 
     super.onBackPressed(); 
} 
     } 

và điều cuối cùng: để vô hiệu hóa poping của bàn phím standart của android tôi đã tạo một CustomEditText mà chỉ đơn giản trả về false tại địa chỉ: onCheckIsTextEditor (), đây là lớp CustomEditText:

package com.galrom.keyboard; 
import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.EditText; 

public class EditTextNoKeyBoard extends EditText { 

public EditTextNoKeyBoard(Context context) { 
    super(context); 
} 


public EditTextNoKeyBoard(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 


public EditTextNoKeyBoard(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
public boolean onCheckIsTextEditor() { 
    // TODO Auto-generated method stub 
    return false; 
} 
    } 

Hy vọng nó sẽ giúp bạn ra ngoài ... nếu bạn có đề xuất cải tiến tôi sẽ được hạnh phúc để nghe. Gal.

+2

Điều này đủ điều kiện cho một bài đăng trên blog. – Nirmal

+0

Cảm ơn câu trả lời tuyệt vời vì đã chia sẻ. – Adamski

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