gpt4 book ai didi

android - 如何在webview中搜索文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:18 25 4
gpt4 key购买 nike

我在 android 工作...我是这个领域的新手...我开发了一个 webview...我需要在 webview 中搜索特定文本...我搜索了一些问题,例如 t hi s 但我没有得到正确的答案

我做了这么多代码,但是在执行时显示了 webview,但是我在代码中提供的按钮丢失了

*

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;
}
});


}
}

*

最佳答案

虽然这是一个较旧的问题,但基于 AndroidEnthusiastic 的版本,我设法整理了一个实际上工作得相当不错的问题。我测试过的 Samsung Note 上的按钮类型存在一个小错误,但无论我如何更改 IME 类型,它都变得一团糟。所以我只是隐藏了软键键盘。但是布局更好。

public class SearchDemoActivity extends ActionBarActivity implements View.OnClickListener
{
WebView mWebView;
private RelativeLayout 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.activity_search);
getActionBar().setTitle(R.string.information_display);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://devemat-androidprogramming.blogspot.com/");

nextButton = (Button) findViewById(R.id.nextButton);
closeButton = (Button) findViewById(R.id.closeButton);
findBox = (EditText) findViewById(R.id.findBox);
findBox.setSingleLine(true);
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
{
// Can't use getMethod() as it's a private method
for (Method m : WebView.class.getDeclaredMethods())
{
if (m.getName().equals("setFindIsUp"))
{
m.setAccessible(true);
m.invoke(mWebView, true);
break;
}
}
}
catch (Exception ignored)
{
}
finally
{
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View vv = getCurrentFocus();
if (vv != null)
{
inputManager.hideSoftInputFromWindow(v.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
return false;
}
});
nextButton.setOnClickListener(this);
closeButton.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
return true;
}

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

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

public void search()
{
container = (RelativeLayout) findViewById(R.id.layoutId);
if (container.getVisibility() == RelativeLayout.GONE)
{
container.setVisibility(RelativeLayout.VISIBLE);
}
else if (container.getVisibility() == RelativeLayout.VISIBLE)
{
container.setVisibility(RelativeLayout.GONE);
}
}

@Override
public void onClick(View v)
{
if (v == nextButton)
{
mWebView.findNext(true);
}
else if (v == closeButton)
{
container.setVisibility(RelativeLayout.GONE);
}
}
}

布局 XML (activity_search.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layoutId" />

<RelativeLayout
android:id="@+id/layoutId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone" >

<Button
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Close" />

<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/closeButton"
android:text="Next" />

<EditText
android:id="@+id/findBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/nextButton"
android:hint="Enter search keyword here."
android:singleLine="true" />
</RelativeLayout>

</RelativeLayout>

菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >

<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
custom:showAsAction="always"
android:title="@string/action_search"/>
</menu>

关于android - 如何在webview中搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16849990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com