如何寻找在web视图文本视图、文本、web

2023-09-05 08:03:05 作者:♚  盜心病人

我在机器人的工作......我在这领域的新手...我开发一个web视图......我需要搜索的WebView内特定的文字...我搜索关于如T his但我没有得到一个正确的答案

I am working in android... I am a newbie in this field... I developed a webview...i need to search for a particular text inside the webview... I searched about some questions like this but i didn't get a proper answer

我做了这么多的code,但执行时的WebView被显示,但按键我给在code缺少

I did this much code but when executed the webview is showing but the buttons i gave in the code are missing

*

private static final int SEARCH_MENU_ID = Menu.FIRST;  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu){  
    super.onCreateOptionsMenu(menu);  

    menu.add(0, SEARCH_MENU_ID, 0, "Search");  

    return true;  
    }  

    public boolean onPrepareOptionsMenu(Menu menu){  
    super.onPrepareOptionsMenu(menu);  
    return true;  
    }  

    public boolean onOptionsItemSelected(MenuItem item){  
    switch(item.getItemId()){  
    case SEARCH_MENU_ID:  
    search();  
    return true;  
    }  
    return true;  
    }  

    public void search(){  
    container = (LinearLayout)findViewById(R.id.lLayout1);  

    nextButton = new Button(this);  
    nextButton.setText("Next"); 
    nextButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            mWebView.findNext(true); 
            // TODO Auto-generated method stub

        }
    });
    container.addView(nextButton);  

    closeButton = new Button(this);  
    closeButton.setText("Close");
    closeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {




            container.removeAllViews();
            // TODO Auto-generated method stub

        }
    });


    container.addView(closeButton);  

    findBox = new EditText(this);  
findBox.setMinEms(30);  
findBox.setSingleLine(true);  
findBox.setHint("Search"); 
findBox.setOnKeyListener(new OnKeyListener(){  
public boolean onKey(View v, int keyCode, KeyEvent event){  
if((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))){  
mWebView.findAll(findBox.getText().toString());  

try{  
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);  
m.invoke(mWebView, true);  
}catch(Exception ignored){}  
}  
return false;  
}  
});


    }
}

*

推荐答案

请试试这个段

  package com.search.demo;  


 import java.lang.reflect.Method;  

 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.KeyEvent;  
 import android.view.Menu;  
 import android.view.MenuItem;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.view.View.OnKeyListener;  
 import android.webkit.WebView;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.LinearLayout;  

 public class SearchDemoActivity extends Activity {  
  WebView mWebView;  
  private LinearLayout container;  
  private Button nextButton, closeButton;  
 private EditText findBox;  
/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    mWebView = (WebView)findViewById(R.id.webview);  
    mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/");  
}  

private static final int SEARCH_MENU_ID = Menu.FIRST;  

@Override  
public boolean onCreateOptionsMenu(Menu menu){  
super.onCreateOptionsMenu(menu);  

menu.add(0, SEARCH_MENU_ID, 0, "Search");  

return true;  
}  

public boolean onPrepareOptionsMenu(Menu menu){  
super.onPrepareOptionsMenu(menu);  
return true;  
}  

public boolean onOptionsItemSelected(MenuItem item){  
switch(item.getItemId()){  
case SEARCH_MENU_ID:  
search();  
return true;  
}  
return true;  
}  

public void search(){  
container = (LinearLayout)findViewById(R.id.layoutId);  

nextButton = new Button(this);  
nextButton.setText("Next");  
nextButton.setOnClickListener(new OnClickListener(){  
@Override  
public void onClick(View v){  
mWebView.findNext(true);  
}  
});  
container.addView(nextButton);  

closeButton = new Button(this);  
closeButton.setText("Close");  
closeButton.setOnClickListener(new OnClickListener(){  
@Override  
public void onClick(View v){  
container.removeAllViews();  

 }  
});  
container.addView(closeButton);  

findBox = new EditText(this);  
findBox.setMinEms(30);  
findBox.setSingleLine(true);  
findBox.setHint("Search");  

findBox.setOnKeyListener(new OnKeyListener(){  
public boolean onKey(View v, int keyCode, KeyEvent event){  
if((event.getAction() == KeyEvent.ACTION_DOWN) && ((keyCode == KeyEvent.KEYCODE_ENTER))){  
mWebView.findAll(findBox.getText().toString());  

try{  
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);  
m.invoke(mWebView, true);  
}catch(Exception ignored){}  
}  
return false;  
}  
});  

container.addView(findBox);  
}  
}