可搜索的Android词典

时间:2016-07-17 10:10:25

标签: android dictionary searchview

我需要创建一个像字典一样的应用程序。

用户可以使用工具栏中的SearchView小部件从文本文件中搜索单词的含义。在我的代码中,我只能在文本文件中显示整个内容,而我无法执行查询。

有人可以帮帮我吗?我一直在苦苦挣扎很长一段时间没有线索。

以下是我的代码(到目前为止我尝试过的):

package issa.example.com.searchview;

        import android.app.SearchManager;
        import android.content.Context;
        import android.content.Intent;
        import android.graphics.Color;
        import android.net.Uri;
        import android.os.Bundle;
        import android.support.v7.app.ActionBarActivity;
        import android.support.v7.widget.SearchView;
        import android.view.Menu;
        import android.view.MenuInflater;
        import android.view.MotionEvent;
        import android.view.View;
        import android.webkit.WebSettings;
        import android.webkit.WebView;
        import android.widget.SeekBar;
        import android.widget.TextView;
        import android.widget.Toast;

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;

public class SearchResultsActivity extends ActionBarActivity {
    private TextView display;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
        //handleIntent(getIntent());

      Intent searchIntent = getIntent();
        if (Intent.ACTION_SEARCH.equals(searchIntent.getAction())){

            String query = searchIntent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(SearchResultsActivity.this, query, Toast.LENGTH_SHORT).show();

try {
                InputStream is = getAssets().open("dictionary");


                int size = is.available();

                // Read the entire asset into a local byte buffer.
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();

                // Convert the buffer into a string.
                String text = new String(buffer);

                // Finally stick the string into the text view.
                // Replace with whatever you need to have the text into.

                TextView tv = (TextView)findViewById(R.id.text);
                tv.setText(text);

            } catch (IOException e) {
                // Should never happen!
                throw new RuntimeException(e);
            }

        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        return true;
    }


 }
}

0 个答案:

没有答案