Android使用自定义数字键盘,如何创建删除键?

时间:2014-01-21 15:00:28

标签: android keyboard

我创建了一个数字键盘,它将文本输入EditText框,我有工作,但不知道如何编码删除按钮。我需要它一次删除一个字符。

以下是代码示例:

public class QOQuickOrder extends Activity implements OnClickListener {

private final String MY_DATABASE_NAME = "/sdcard/hdw";

private EditText qoItemNumber;
private EditText qoOrderQty;
private TextView qoItemDesc;

Cursor c;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.qo_quickorder_layout);

    qoItemNumber = (EditText) findViewById(R.id.etqoItemNumEntry);
    qoOrderQty = (EditText) findViewById(R.id.etqoItemQtyEntry);
    qoItemDesc = (TextView) findViewById(R.id.tvqoItemDesc);

    qoItemNumber.selectAll();
    qoItemNumber.setText("");

    Button oneButton = (Button) findViewById(R.id.btn1);
    Button twoButton = (Button) findViewById(R.id.button2);
    Button threeButton = (Button) findViewById(R.id.button3);
    Button fourButton = (Button) findViewById(R.id.button4);
    Button fiveButton = (Button) findViewById(R.id.button5);
    Button sixButton = (Button) findViewById(R.id.button6);
    Button sevenButton = (Button) findViewById(R.id.button7);
    Button eightButton = (Button) findViewById(R.id.button8);
    Button nineButton = (Button) findViewById(R.id.button9);
    Button zeroButton = (Button) findViewById(R.id.button0);

    oneButton.setOnClickListener(this);
    twoButton.setOnClickListener(this);
    threeButton.setOnClickListener(this);
    fourButton.setOnClickListener(this);
    fiveButton.setOnClickListener(this);
    sixButton.setOnClickListener(this);
    sevenButton.setOnClickListener(this);
    eightButton.setOnClickListener(this);
    nineButton.setOnClickListener(this);
    zeroButton.setOnClickListener(this);

    qoItemNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if (hasFocus = true){

                getItemDesc();
            }

        }
    });

}

public void onClick(View v){


    qoItemNumber.append(String.valueOf(v.getTag()));



}

1 个答案:

答案 0 :(得分:0)

只需使用以下方法一次删除一个编辑文本的最后一个字符:

String str = youredittext.getText().toString().trim();
if(str.length()!=0){
str  = str.substring( 0, str.length() - 1 ); 
youredittext.setText ( str );

如果您对此有任何疑问,请与我们联系。