如何在editText框中添加常量文本?

时间:2016-03-17 15:23:54

标签: android android-edittext radio-button radio-group

有三个单选按钮: 1. NID 2.护照3.驾驶执照

之后会有一个编辑文本框,用户可以在其中编写已选择的ID。如果用户选择NID,NID-将显示在editText框中,该框不可移动,则用户可以在editText框中写入12345678,它将保存在数据库中,如NID-12345678。这是我的代码:

  1. 单选按钮操作:

     <root>
       <item>
         <country>Brobdingnag</country>
         <state>State of Fiction</state>
       </item>
       <item>
         <test></test>
         <country>Lilliput</country>
       </item>
     </root>
    

    }

  2. 数据库部分:

    public void RadioButtonClicked(View view) {
    
    
    String selectedID = "";
    boolean checked = ((RadioButton) view).isChecked();
    
    switch (view.getId()) {
        case R.id.uNid:
            if (checked)
                selectedID = "NID";
            Toast.makeText(Registration.this, "NID", Toast.LENGTH_LONG).show();
           // etID.setText("NID");
            break;
        case R.id.uPassport:
            if (checked)
                selectedID = "Passport";
            Toast.makeText(Registration.this, "Passport", Toast.LENGTH_LONG).show();
           // etID.setText("Passport");
            break;
    
        case R.id.uLicense:
            if (checked)
                selectedID = "Driving License";
            Toast.makeText(Registration.this, "Driving License", Toast.LENGTH_LONG).show();
            // etID.setText("Driving License");
            break;
    }
    

2 个答案:

答案 0 :(得分:0)

您需要使用EditText.seteditable(false);属性。 如果要限制用户在editText中编写内容。 只有editText的文本可以从我们的java文件中设置。

答案 1 :(得分:0)

我解决了这个问题。这是我的单选按钮代码:

 public void RadioButtonClicked(View view) {



    String selectedID = "";

    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()) {
        case R.id.uNid:
            if (checked)
                selectedID = "NID";
            Toast.makeText(Registration.this, "NID", Toast.LENGTH_LONG).show();
            etID.setText("n ");
            Selection.setSelection(etID.getText(), etID.getText().length());
            etID.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                                          int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!s.toString().startsWith("n ")) {
                        etID.setText("n ");
                        Selection.setSelection(etID.getText(), etID
                                .getText().length());

                    }

                }

            });
           // etID.setText("NID");
            break;
        case R.id.uPassport:
            if (checked)
                selectedID = "Passport";
            Toast.makeText(Registration.this, "Passport", Toast.LENGTH_LONG).show();
           // etID.setText("Passport");
            etID.setText("p ");
            Selection.setSelection(etID.getText(), etID.getText().length());
            etID.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                                          int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!s.toString().startsWith("p ")) {
                        etID.setText("p ");
                        Selection.setSelection(etID.getText(), etID
                                .getText().length());

                    }

                }

            });
            break;

        case R.id.uLicense:
            if (checked)
                selectedID = "Driving License";
            Toast.makeText(Registration.this, "Driving License", Toast.LENGTH_LONG).show();
            // etID.setText("Driving License");
            etID.setText("d ");
            Selection.setSelection(etID.getText(), etID.getText().length());
            etID.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                                          int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!s.toString().startsWith("d ")) {
                        etID.setText("d ");
                        Selection.setSelection(etID.getText(), etID
                                .getText().length());

                    }

                }

            });
            break;
    }
}