1

在句子中找到一个单词后,我希望用户能够清除他的条目并在编辑文本中再次键入一个句子。

这是我到目前为止所尝试的:

 final String[] words = {"cowboy", "animal"};
 final String[] meanings = { "meaning1", "meaning2" };
 Boolean check = false;

 private void initControls() {
    // TODO Auto-generated method stub
    text = (EditText) findViewById (R.id.editText1);

    view = (TextView) findViewById (R.id.textView1);

    clear = (Button) findViewById (R.id.button2);

    clear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            text.setText("");
            view.setText("");
        }
    });

    ok = (Button) findViewById (R.id.button1);
    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            findAmbiguousWord();
        }   
    });
}

private void findAmbiguousWord(){
    String string = text.getText().toString();

    int index = 0;

    for (int i = 0; i < words.length; i++) {
        if (string.toLowerCase().contains(words[i].toLowerCase())) {
            check = true;
            index = i;
        } 
    }

    view.setText(check ? meanings[index] : "No ambiguous word/s found.");
}

当我试图清除我的条目并再次输入新条目时,显示的结果相同。应该怎么做才能避免显示以前的结果?任何帮助深表感谢。

4

2 回答 2

1

将检查功能提取到这样的方法中:

ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        checkAmbiguousWord();
    }   
});  

...

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    Integer ambiguousIndex = findAmbiguousWordIndex(textToCheck);
    view.setText(ambiguousIndex != null ? meanings[ambiguousIndex] : "No ambiguous word/s found.");
}

/**
 * @param text checked for ambguous words
 * @return the index of the ambiguous word in the {@code words} array or
 * null if no ambiguous word is found
 */
private Integer findAmbiguousWordIndex(String text) {
    final String lowerCasedText = text.toLowerCase();
    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            return i;
        }
    }
    return null;
}

check这消除了对隐藏的内部状态(变量)的依赖。这种编程风格允许您分离视图控制器代码和业务功能,并最终独立于视图为业务功能编写测试。

更新:要显示多个模棱两可的词,请使用索引列表

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    List<Integer> ambiguousIndexes = findAmbiguousWordIndexes(textToCheck);
    view.setText(!ambiguousIndexes.isEmpty() ? ambigousIndexesToMessage(ambiguousIndexes) : "No ambiguous word/s found.");
}

    public String ambigousIndexesToMessage(List<Integer> ambiguousIndexes) {
        // create the text using the indexes
        // this is an example implementation
        return ambiguousIndexes.toString(); // creates a list of "1","2",...
    }

/**
 * @param text checked for ambguous words
 * @return the list of indexes of the the ambiguous words in the {@code words} array          
 */
private List<Integer> findAmbiguousWordIndexes(String text) {
    final String lowerCasedText = text.toLowerCase();
    final List<Integer> ambiguousWordIndexList = new ArrayList<Integer>();
    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            ambiguousWordIndexList.add(i);
        }
    }
    return ambiguousWordIndexList;
}
于 2013-08-26T13:09:00.483 回答
0

您需要在方法开始时将检查设置为 false。

于 2013-08-25T19:59:36.863 回答