无法在点击ImageView时添加更多网站

时间:2017-02-15 04:23:17

标签: android-studio webview

我想用不同的网址添加3个Imageview,我尝试添加它,但这对我来说是不可能的。有什么建议吗?如果你们帮我解决这个问题会很棒。

主要活动

Set<Character> uniqueChars = new HashSet<Character>();
Set<Character> repeats = new HashSet<Character>();
for(int i = 0; i < str.length() - 1; i++) {
    if (!uniqueChars.add(str.charAt(i)) {
        repeats.add(str.charAt(i));
    }
}

// now loop through the repeats set and print out each one.

主要活动的XML布局

public class main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    ImageView img = (ImageView) findViewById(R.id.amazon);
    img.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent  = new Intent(main.this, MainActivity.class);
            intent.setData(Uri.parse("http://www.amazon.in"));
            startActivity(intent);
        }

    });



}

网络视图活动

<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="@drawable/amazon"
android:layout_marginTop="19dp"
android:id="@+id/amazon"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="11dp" />

<ImageButton
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:srcCompat="@drawable/flipkart"
    android:id="@+id/flipkart"
    android:layout_marginStart="31dp"
    android:layout_alignTop="@+id/amazon"
    android:layout_toEndOf="@+id/amazon" />

<TextView
    android:text="Amazon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/amazon"
    android:layout_toStartOf="@+id/flipkart"
    android:id="@+id/tvam" />

<TextView
    android:text="  flipkart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/flipkart"
    android:layout_alignStart="@+id/flipkart"
    android:id="@+id/tvflip" />

我的webview xml

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    WebView htmlWebView = (WebView) findViewById(R.id.webView);
    htmlWebView.setWebViewClient(new CustomWebViewClient());
    WebSettings webSetting = htmlWebView.getSettings();
    webSetting.setJavaScriptEnabled(true);
    webSetting.setDisplayZoomControls(true);
    htmlWebView.loadUrl("https://amazon.in");





}



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

1 个答案:

答案 0 :(得分:0)

Here is the full code.Make one change that take your imagebutton insted of simple button .
**main.xml**
  <Button
        android:id="@+id/btnAmazon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AMZON" />

    <Button
        android:id="@+id/btnFlipkart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FLIPKART" />


    <Button
        android:id="@+id/btnGoogle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="GOOGLE" />

**Code for java file main.java**



    btnGoogle = (Button) findViewById(R.id.btnGoogle);
            btnAmazon = (Button) findViewById(R.id.btnAmazon);
            btnFlipkart = (Button) findViewById(R.id.btnFlipkart);

            final Bundle bundle = new Bundle();
            btnGoogle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(SplashActivity.this, WebviewUrl.class);
                    bundle.putString("URL","http://www.google.com");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

            btnAmazon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(SplashActivity.this, WebviewUrl.class);
                    bundle.putString("URL","http://www.amazon.in");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

            btnFlipkart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent  = new Intent(SplashActivity.this, WebviewUrl.class);
                    bundle.putString("URL","https://www.flipkart.com/");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

**xml code for MainActivity**
<?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:id="@+id/activity_webview_url"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.pg.gymapp.activity.WebviewUrl">

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


**code for mainactivity.java**
public class MainActivity extends AppCompatActivity {
    WebView webView ;

    Bundle bundle ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        webView = (WebView) findViewById(R.id.webView);


        bundle = getIntent().getExtras();

        Log.d("URL ",bundle.getString("URL"));


        webView.loadUrl(bundle.getString("URL"));

        webView.setWebViewClient(new CustomWebViewClient());
        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDisplayZoomControls(true);
        webView.loadUrl(bundle.getString("URL"));
    }

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


At last make sure that you gave **internet permission** in manifest file
 <uses-permission android:name="android.permission.INTERNET" />
Hope this will help you .