Java 在setOnClickListener上应用WebView

Java 在setOnClickListener上应用WebView,java,android,Java,Android,目前我是android的初学者。现在,我正试图使我的ListView项目在单击任何项目时打开一个包含WebView的布局。每个项目将指向不同的页面。但我不确定我如何才能做到这一点。这是我的密码 @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflate

目前我是android的初学者。现在,我正试图使我的ListView项目在单击任何项目时打开一个包含WebView的布局。每个项目将指向不同的页面。但我不确定我如何才能做到这一点。这是我的密码

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.activity_listview, null);
    }

    FeedItem p = getItem(position);

    if (p != null) {
        TextView tt1 = (TextView) v.findViewById(R.id.title_text);
        TextView tt2 = (TextView) v.findViewById(R.id.date_text);

        if (tt1 != null) {
            tt1.setText(p.getTitle());
        }


        if (tt2 != null) {
            tt2.setText(p.getPubDate());
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                FeedItem q = getItem(position);
                if(q.getLink() != null){
                //Toast.makeText(getContext(), "" + q.getLink(), Toast.LENGTH_SHORT).show();

                }
            }
        });

    }




    return v;

}
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
拉平机vi;
vi=LayoutInflater.from(getContext());
v=vi.充气(R.layout.activity_listview,空);
}
FeedItem p=getItem(位置);
如果(p!=null){
TextView tt1=(TextView)v.findViewById(R.id.title\u text);
TextView tt2=(TextView)v.findViewById(R.id.date\u text);
如果(tt1!=null){
tt1.setText(p.getTitle());
}
如果(tt2!=null){
tt2.setText(p.getPubDate());
}
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
FeedItem q=getItem(位置);
如果(q.getLink()!=null){
//Toast.makeText(getContext(),“”+q.getLink(),Toast.LENGTH_SHORT).show();
}
}
});
}
返回v;
}

您可以根据您的要求进行少量修改,并需要将导入文件放入您的包中

if(q.getLink() != null){
            Intent intent = new Intent(getActivity(), BrowserActivity.class);
            intent.putExtra("url", q.getLink());
            startActivity(intent);
}
浏览活动

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.yourproject.R;


public class BrowserActivity extends FragmentActivity {

    WebView webview;

    public BrowserActivity() {
        // Required empty public constructor
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.comment_detail);

        String url = getIntent().getStringExtra("url");
        Log.d("URL ", url);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setSupportZoom(true);

        webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {

            public void onPageStarted(WebView view, String url) {

            }

            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                MyApplication.hideTransparentProgressDialog();
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(CommentDetails.this, description, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
注释_detail.xml

<include
    android:id="@+id/headerTV"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    layout="@layout/title_bar_layout_job_info" />

<WebView
    android:layout_below="@+id/headerTV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webview">

</WebView>



你的问题完全是基于观点的。两者都可以根据应用程序的要求使用

默认浏览器 只需使用4行或4行文件就很容易了,而且很好用。但它让用户脱离了应用程序

如何使用它。

Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(YOUR_URL));
startActivity(intent);
网络视图 可以用作应用程序的一部分。您需要在包含webview的活动上创建,并从要打开webview的位置调用intent

示例

布局

<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">

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

</RelativeLayout>
只需从您想要打开的
Webview
的位置调用它即可

Intent intent= new Intent(MainActivity.this,WebViewActivity.class);
startActivity(intent);
参考这个


我希望它是清楚的。

要打开浏览器使用意图:

String url = "https://www.google.com/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
以及:

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

您也可以在WebView上加载自己的html文件。

尝试以一种很好的方式提问,明确所有需求和问题,然后发布您的问题,因为这会影响您的声誉
<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");

    }

}