浏览器的搜索框的简易制作
首先在浏览器中定义一个简单的布局
一个搜索栏,一个前进的图标,一个WebView
其代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv_go"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/jiantou"
/>
<WebView
android:layout_below="@id/et_path"
android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
之后再mainActivity中写相应的逻辑:
public class MainActivity extends Activity {
private EditText et_path;
private ImageView iv_go;
private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
iv_go = (ImageView) findViewById(R.id.iv_go);
wv = (WebView) findViewById(R.id.wv);
Intent intent = getIntent();
if (intent != null) {
if (intent.getData() != null) {
String path = intent.getData().toString();
wv.loadUrl(path);
et_path.setText(path);
}
}
iv_go.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String path = et_path.getText().toString().trim();
wv.loadUrl(path);
}
});
}
}