0

我正在制作一个 ide 并使用 TextView 根据我的 EditText 的行数显示行号。我可以添加新行但如何检查是否已删除行并减少 TextView 的行数?

et.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
        lines++;
        lineNumbers.append(lines+"\n");            
    }
    return false;
}

});

4

1 回答 1

0

这完成了工作。

private void openFile(final File aFile) {
        String nullChk = et.getText().toString();
        exists = true;

        if (!changed || nullChk.matches("")) {
            try {
                currentFile = aFile;
                et.setText(new Scanner(currentFile).useDelimiter("\\Z").next());
                lineNumbers.setText("");
                lines = 1;

                for (int i = 1; i < et.getLineCount(); i++) {
                    lineNumbers.append(lines + "\n");
                    lines++;
            ...
}

et.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    lines++;
                    lineNumbers.append(lines + "\n");
                }
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_DEL)) {
                    lineNumbers.setText("");
                    lines = 0;

                    for (int i = 1; i < et.getLineCount(); i++) {
                        lines++;
                        lineNumbers.append(lines + "\n");
                    }
                }
                return false;
            }
        });
于 2013-04-04T23:49:57.720 回答