禁止单个Edittext的Keyboard弹出窗口

时间:2016-05-09 11:42:22

标签: android

长话短说,我目前有一个日期选择器对话框,弹出单击以获取edittext属性。一切正常,除非弹出对话框,软键盘也会弹出 我不想为页面上的其他属性禁用键盘,我不想保持原样,因为它不仅看起来邋and和笨重,而且意味着用户可以搞砸我可爱的格式化日期对象这很适合我的数据库。不好。

我已经尝试禁用我的XML文件中的元素既可点击又可编辑,但无济于事。下面的代码,像往常一样。

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editTextExpiryDate"
    android:inputType="date"
    android:hint="Card Expiry Date"
    android:editable="false"
    android:clickable="false"
    android:layout_below="@+id/editTextSecurityCode"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/spinnerCardType"
    android:layout_alignEnd="@+id/spinnerCardType" />

然后是(有点凌乱)Java文件,但相关部分是粗体。

public class RegisterFragmentPaymentInfo extends Fragment {

public Spinner creditCardSpinner;
public EditText cardNumberInput;
**public EditText expiryDateInput;**
public EditText securityCodeInput;
public Button proceedButton;

public String cardType;
public String cardNumber;
**public Date expiryDate;**
public int securityCode;

Bundle oldUserData;
Calendar c = Calendar.getInstance();


**DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, monthOfYear);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateDateInput();
    }
};
private void updateDateInput(){
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yy");
    expiryDateInput.setText(sdf.format(c.getTime()));
}**

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_register_payment_info, container, false);

    findInputFields(rootView);

    ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(this.getActivity(), R.array.credit_card_array, android.R.layout.simple_spinner_item);
    a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    creditCardSpinner.setAdapter(a);
    creditCardSpinner.setPrompt("Please select your Card Type");

    proceedButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            readUserInput();

            if (isValidInput()) {
                Bundle userData;
                userData = prepareDataToPass(oldUserData);

                RegisterFragmentConfirm targetFragment = new RegisterFragmentConfirm();
                targetFragment.setArguments(userData);
                FragmentTransaction t = getFragmentManager().beginTransaction();
                t.replace(R.id.fragment_login_main, targetFragment);
                t.addToBackStack(null);
                t.commit();

            }

        }
    });



    e**xpiryDateInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(getActivity(), date, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
        }
    });**


    return rootView;
}

public void findInputFields(View tempView){
    creditCardSpinner = (Spinner) tempView.findViewById(R.id.spinnerCardType);
    cardNumberInput = (EditText)tempView.findViewById(R.id.editTextCardNumber);
    expiryDateInput = (EditText)tempView.findViewById(R.id.editTextExpiryDate);
    securityCodeInput = (EditText) tempView.findViewById(R.id.editTextSecurityCode);
    proceedButton = (Button) tempView.findViewById(R.id.btnCompleteRegistrationStepThree);

    expiryDateInput.setFocusable(false);
}

public void readUserInput(){
    cardType = creditCardSpinner.getSelectedItem().toString();
    cardNumber = cardNumberInput.getText().toString();

    try{
        if(securityCodeInput.getText().toString()!=null){
            if(securityCodeInput.getText().toString().length()==3||securityCodeInput.getText().toString().length()==4){
                securityCode = Integer.parseInt(securityCodeInput.getText().toString());
            }
            else {
                Toast.makeText(getActivity(), "Security Number invalid, please enter a valid Security Number", Toast.LENGTH_LONG).show();
            }
        }else{
            Toast.makeText(getActivity(), "Security Number is mandatory, please enter a Security Number", Toast.LENGTH_LONG).show();
        }
    }catch (NumberFormatException e){
        Toast.makeText(getActivity(), "Security Number must be numeric, please enter a valid Security Number", Toast.LENGTH_LONG).show();
    }

    SimpleDateFormat s = new SimpleDateFormat("dd/MMM/yy");
    try {
        expiryDate = s.parse(expiryDateInput.getText().toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
public Boolean isValidInput(){
    Boolean inputIsValid = false;

    //Boolean validCardType; //Unnecessary; User cannot select other than supplied card types
    Boolean validCardNumber = false;
    Boolean validExpiryDate;
    //Boolean validSecurityCode; //Also unnecessary, validated upon initial input for simplicity.
    Date currentDate = new Date();
    String dateIntermediate;
    Calendar c = Calendar.getInstance();

    if (!cardNumber.isEmpty()){
        validCardNumber = true;
    }else if(cardNumber.isEmpty()){
        validCardNumber = false;
        Toast.makeText(getActivity(), "Card Number is mandatory, please enter a Card Number", Toast.LENGTH_LONG).show();
    }

    SimpleDateFormat s = new SimpleDateFormat("dd/MMM/yy");
    dateIntermediate = s.format(c.getTime());
    try {
        currentDate = s.parse(dateIntermediate);
    } catch (ParseException e) {
        e.printStackTrace();
    }


    if(expiryDate.after(currentDate)){
        validExpiryDate =true;
    }
    else{
        validExpiryDate = false;
        Toast.makeText(getActivity(), "Card has expired, please use a Card that is within date, or check your given expiration is correct", Toast.LENGTH_LONG).show();
    }

    if(validExpiryDate&&validCardNumber){
        inputIsValid = true;
    }

    return inputIsValid;
}

public Bundle prepareDataToPass(Bundle previousUserData){
    Bundle newBundle = new Bundle();
    newBundle.putAll(previousUserData);

    newBundle.putString("card_type", cardType);
    newBundle.putString("card_number", cardNumber);
    newBundle.putSerializable("expiry_date", expiryDate);
    newBundle.putInt("security_code", securityCode);

    return newBundle;
}

}

3 个答案:

答案 0 :(得分:1)

最简单的方法是在你的androidmanifest.xml上进行所需的活动添加这一行

template <class T>
void VecRead(istream& in, std::vector<T>& vector)
{
    size_t size = 0;
    in.read((char*)&size, sizeof(size));
    vector.resize(size);

    for (auto &element : vector)
    {
        readElement(in, element);
    }
}

所以它看起来像:

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize" 

或者您可以将此行添加到onCreat以设置键盘隐藏的par默认值

<activity
        android:name="com.yourdomain.user.AboutActivity"
        android:windowSoftInputMode="stateHidden|adjustPan|adjustResize" />

答案 1 :(得分:0)

  

public void setFocusable (boolean focusable)

设置   这个观点是否能够得到关注。将此设置为false将   还要确保此视图在触摸模式下无法对焦。

因此,当您为EditText设置focusable false时。它仍然可以检测到点击事件,但它无法打开键盘,因为它无法获得焦点。

答案 2 :(得分:0)

xml

中创建虚拟布局
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0dp" 
    android:layout_height="0dp"/>