0

3つの画像/アイコンを含むGridViewレイアウトがあります。私がやろうとしているのは、アイテムがクリックされるたびに、Webサイトページ(Webview)が表示されるようにすることです。これが私がしたことです:

  1. main.xmlファイルで、LinearLayoutをルート要素としてGridViewおよびWebViewレイアウトを定義しました。
  2. Androidmanifest.xmlファイルでインターネットアクセスの許可が与えられました
  3. gridviewのコードを記述
  4. Webviewのコードを書きました。
  5. GridViewのsetOnItemClickListenerの下で、loadUrl()を使用して表示されるWebページのURLを指定しました。

それで、質問:「アイコン/画像がクリックされたときに表示されるWebページを作成するにはどうすればよいですか?」 エミュレーターでUIを見ることができます。ただし、アイコンをクリックしても応答はありません。私が推測するonClickItem()イベントで何か問題が発生しました。

お時間をいただきありがとうございます。私は次のコードを書きました:

package prototype.wenview.gridview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity 
{
    // Images to display-----------------------//
    Integer[] imageIds =    {           R.drawable.market,          R.drawable.news,            R.drawable.weather    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState); //calls the method from super class Activity
        setContentView(R.layout.main);          //sets the UI

        GridView gridView = (GridView) findViewById(R.id.gridview);// Bring gridView into context from main.xml
        gridView.setAdapter(new ImageAdapter(this)); 

        gridView.setOnItemClickListener(new OnItemClickListener()
                    {
                        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
                        {
                WebView myWebView = (WebView)findViewById(R.id.webview);
        myWebView.loadUrl("http://translate.google.com/translate?sl=auto&tl=hi&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.kvk.pravara.com%2Fweather.html");
                        }
                    }
                    );  //setOnItemClickListener declaration ends here.
     }  //onCreate() ends here.

    public class ImageAdapter extends BaseAdapter
    {
        private Context context; // Context class object - context

        public ImageAdapter(Context c)
        {
            context = c;
        }

        //return number of images
        public int getCount()
        {
            return imageIds.length;
        }

        public Object getItem(int position)     // return type of getItem method is an Object
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        //returns an ImageView view--------------
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView;
            if(convertView == null)
            {
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(5, 5, 5, 5);
            }

            else
            {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(imageIds[position]);
            return imageView;
        }       
    }    
}

XMLコード:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

/>

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>    

</LinearLayout>

/*Activity2.javaのXMLコード。このページは、グリッドビューのアイコンがクリックされた後にWebページを表示することを目的としています。* /

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

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

//アクティビティ2のJavaコード

パッケージin.niteesh.MultipleActivities;

import android.app.Activity; android.os.Bundleをインポートします。

public class Activity2 extends Activity 
{   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {   
        super.onCreate(savedInstanceState);         //calls the method from super class Activity
        setContentView(R.layout.activity2);          //sets the UI
    }
}

//Activity2の存在について言及したAndroidManifest.xmlファイル

<activity android:name=".Activity2" android:label="Activity 2">
            <intent-filter>
                <action android:name="net.learn2develop.ACTIVITY2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

// onItemClickのMainActivityクラスで、2番目のアクティビティに表示されるWebページ用に次のコードを記述しました

public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String url = "http://google.com/";  
        Intent i = new Intent(Intent.ACTION_VIEW);  
        i.setData(Uri.parse(url));  
        startActivity(new Intent("net.learn2develop.ACTIVITY2"));//reference to start the activity
}

/問題は-アイコンをクリックすると、Activity2に切り替わりますが、Webページが表示されません。さまざまなターンアラウンドを試しましたが、解決策が見つかりませんでした/

4

3 に答える 3

1

xmlコードでは、Webビューとグリッドビューの高さは親の塗りつぶしです。ページをロードできる必要がありますが、gridviewは完全に表示され、全画面を占めます。

コンテンツをラップします。

また、要件を満たしている場合は、次のアクティビティでWebビューをロードする方がよいと思います。

于 2012-04-07T18:04:49.127 に答える
1

Agarwalは正しいです-レイアウトを修正するか、代わりに別のアクティビティでWebページを開くことを検討してください。

URLからWebページを開くためのコードは次のとおりです。

String url = "http://almondmendoza.com/android-applications/";  
Intent i = new Intent(Intent.ACTION_VIEW);  
i.setData(Uri.parse(url));  
startActivity(i);

編集:

新しいアクティビティからリンクを開くには:

String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(this, Activity2.class);
i.putExtra("key_url", url);
startActivity(i);

thisinnew Intent(this, Activity2.class);はあなたの活動の文脈です。このコードが内部クラス内にある場合は、MyActivity1.this

startActivityActivity2の場合、前のコードからIntentによってスローされたURLを取得するには、

String url = getIntent().getStringExtra("key_url");

次に、URLを使用して、次のように好きなことを行うことができます。

webView.loadUrl(url);

これらすべてのonCreate

于 2012-04-08T03:17:27.207 に答える
1

私はあなたの活動の中でウェブページを開くための解決策を持っています:

まず第一に、私はあなたのレイアウトのいくつかのプロパティを変更しました:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

それ以外の場合、WebViewは表示されません。

また、OSを制御するためのクラスを追加する必要があります。

private class CustomWebViewClient extends WebViewClient {   
    @Override
    /**
     * Take control over the loaded url so the url is open in the current Webview and not by the OS browser
     */
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }

}

そして、リスナーは次のようになります。

public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {
        WebView myWebView = (WebView)findViewById(R.id.webview);
        CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
        myWebView.setWebViewClient(myCustomWebViewClient);
        myWebView.loadUrl("http://www.google.com");
    }

それがあなたが探しているものであることを願っています。

編集:(アプリケーションの)2番目のアクティビティ内でWebビューを開く別のソリューション:

リスナーに変更を加えました:

public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
    String url;
    //The url changes accordingly to the button clicked
   switch (position) {
        case 0:  url = "http://stackoverflow.com";
                 break;
        case 1:  url = "http://yahoo.fr";
                 break;
        case 2:  url = "http://bing.com";
                 break;
        default: url = "http://google.com";
                 break;
    }
    Intent intent = new Intent(MainActivity.this,SecondActivity.class); 
    //used to pass the url to the next activity
    intent.putExtra("url", url);
    startActivity(intent);//reference to start the activity
}

私の2番目の活動:

public class SecondActivity extends Activity 
{   
    @Override
    public void onCreate(Bundle savedInstanceState)
    {   
        super.onCreate(savedInstanceState);         //calls the method from super class Activity
        setContentView(R.layout.second);          //sets the UI
        //the webview inside the second activity xml
        WebView myWebView = (WebView)findViewById(R.id.webview);
        CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
        myWebView.setWebViewClient(myCustomWebViewClient);
        //get the url from the previous activity
        String url = getIntent().getStringExtra("url");
        myWebView.loadUrl(url);
    }

    private class CustomWebViewClient extends WebViewClient {   
        @Override
        /**
         * Take control over the loaded url so the url is open in the current Webview and not by the OS browser
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    }

}

xmlファイルには触れませんでした。

最初の解決策として、ordreのshouldOverrideUrlLoadingをオーバーライドして、右側のビューにURLをロードする必要があります。

于 2012-04-09T14:42:26.183 に答える