0

電話の戻るボタンは webview で機能しますが、webview を終了してアプリケーションに移動したい場合、これは何もしませんでした。アプリケーションに戻るボタンを付けましたが、そのままにしませんでした。

ジャワ:

package com.wiralss.adb;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
 import android.view.Window;
 import android.webkit.WebChromeClient;
 import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
 public class about extends Activity implements OnClickListener{
 WebView webView;

  final Activity activity = this;

@Override
public void onCreate(Bundle savedInstanceState)
{


super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.instagrem);
webView = (WebView) findViewById(R.id.webView1);
Button B123=(Button)findViewById(R.id.button1235);
B123.setOnClickListener(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");

webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress)
    {
        activity.setTitle("Loading...");
        activity.setProgress(progress * 100);

        if(progress == 100)
            activity.setTitle(R.string.app_name);

    }
});


webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description,    String failingUrl)
    {
        // Handle the error

    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
        return true;
    }
});

  }



@Override
public void onClick(View v) {
    switch(v.getId()){

    case R.id.button1235:
        Intent B = new Intent(this, MainActivity.class);
    startActivity(B);
    break;

    // TODO Auto-generated method stub

}

    // TODO Auto-generated method stub

}
public void onBackPressed (){

    if (webView.isFocused() && webView.canGoBack()) {
            webView.goBack();       
    }
    else {
            super.onBackPressed();
            finish();
    }
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        webView.goBack();


        return true;
    }
    return false;
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1235"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="580px"
            android:layout_weight="0.05"
            android:text="Button" />

    </LinearLayout>

</RelativeLayout>

別の質問です。ボタンを削除するには、削除する必要がありますか?:

Button B123=(Button)findViewById(R.id.button1);
B123.setOnClickListener(this);

と?

@Override
public void onClick(View v) {
    switch(v.getId()){

    case R.id.button1:
        Intent B = new Intent(this, MainActivity.class);
    startActivity(B);
    break;

    // TODO Auto-generated method stub

}

    // TODO Auto-generated method stub

}

大変お世話になりました。

4

2 に答える 2

3

super.onBackPressed()アクティビティの終了を自分で制御しているため、 への呼び出しは必要ありません。

webView.isFocused()同様に、メソッド呼び出しは実際には必要ありません。

onBackPressedメソッドを次のように書き換えることができます。

public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();       
    }  else {
        finish();
    }
}

あなたのbutton1235ボタンを含める動機についてはよくわかりませんが、削除したいものはありますか?

于 2013-07-11T13:17:30.257 に答える
0

メソッドの実装を削除すると、メソッドの実装が呼び出されないonKeyDown場合があります。のみである必要があります:onBackPressedonBackPressed

public void onBackPressed() {

    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

ボタンを削除するには、それをなくすだけで十分ですが、まず、クラス メンバーとして宣言する必要があります。

private Button B123;

で行うように初期化しますonCreate

webView = (WebView) findViewById(R.id.webView1);
B123 = (Button) findViewById(R.id.button1235);
B123.setOnClickListener(this);

それをなくすには:

@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.button1235:
        B123.setVisibility(View.GONE);
        Intent B = new Intent(this, SecondActivity.class);
        startActivity(B);
        break;

    // TODO Auto-generated method stub

    }

    // TODO Auto-generated method stub

}

pxの代わりに使用しているため、いくつかの糸くずエラーが発生しているため、レイアウトを変更しますdp。全体のレイアウトを改善できます。

于 2013-07-12T12:06:57.943 に答える