classSimpleBrowser

Last-modified: 2015-03-18 (水) 12:30:27
public class SimpleBrowser extends Activity implements OnClickListener{

	EditText url;
	WebView ourBrow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simplebrowser);

		ourBrow = (WebView) findViewById (R.id.wbBrowser);

		ourBrow.getSettings().setJavaScriptEnabled(true);	// javascript OK
		ourBrow.getSettings().setLoadWithOverviewMode(true);
		ourBrow.getSettings().setUseWideViewPort(true);

		ourBrow.setWebViewClient(new ourViewClient());
		try{
			ourBrow.loadUrl("http://www.mybringback.com");
		} catch (Exception e){
			e.printStackTrace();
		}

		Button go = (Button) findViewById (R.id.bGo);
		Button back = (Button) findViewById (R.id.bBack);
		Button refresh = (Button) findViewById (R.id.bRefresh);
		Button forward = (Button) findViewById (R.id.bForward);
		Button clearHistory = (Button) findViewById (R.id.bHistory);
		url = (EditText) findViewById (R.id.etURL);
		go.setOnClickListener(this);
		back.setOnClickListener(this);
		refresh.setOnClickListener(this);
		forward.setOnClickListener(this);
		clearHistory.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){
		case R.id.bGo:
			String theWebsite = url.getText().toString();
			ourBrow.loadUrl(theWebsite);
			// hiding the keybord after using an EditText
			InputMethodManager imm =
                              (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromInputMethod(url.getWindowToken(), 0);
			break;
		case R.id.bBack:
			if(ourBrow.canGoBack())
			ourBrow.goBack();
			break;
		case R.id.bForward:
			if(ourBrow.canGoForward())
			ourBrow.goForward();
			break;
		case R.id.bRefresh:
			ourBrow.reload();
			break;
		case R.id.bHistory:
			ourBrow.clearHistory();
			break;
		}
	}
}




~<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="10">
        <EditText
            android:id="@+id/etURL"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            >
        </EditText>
        <Button
            android:id="@+id/bGo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Go"
            android:layout_weight="8"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="8">
        <Button
            android:id="@+id/bBack"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Go Back a page"
            android:layout_weight="2"/>
        <Button
            android:id="@+id/bForward"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Go Forward"
            android:layout_weight="2"/>
        <Button
            android:id="@+id/bRefresh"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Refresh Page"
            android:layout_weight="2"/>
        <Button
            android:id="@+id/bHistory"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="ClearHistory"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </LinearLayout>
    <WebView
        android:id="@+id/wbBrowser"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ></WebView>
~</LinearLayout>