简体   繁体   English

如何将信息从一个活动的EditText传递到另一个活动,以将URL加载到该新活动的WebView中?

[英]How do I pass info from one activity's EditText to another for the url to be loaded in that new activity's WebView?

all! 所有! I have been searching through StackOverflow for a few hours now and cannot find a solution to my problem. 我已经在StackOverflow上搜索了几个小时,但找不到解决我问题的方法。

I am developing an android app that needs to take input from the user in one activity through an EditText (this happens to be an int value). 我正在开发一个Android应用程序,该应用程序需要通过EditText在一个活动中接受用户的输入(这恰好是一个int值)。 I then need that EditText's int value to be passed to a second activity, and parsed into a url to be loaded into a webview in that second activity. 然后,我需要将EditText的int值传递给第二个活动,并解析为一个url,以加载到该第二个活动中的webview中。 However, I cannot get this to work. 但是,我无法使它正常工作。 In fact, I cannot get my WebView to work at all, even trying to load websites like Google. 实际上,即使尝试加载Google之类的网站,我也无法完全使用WebView。 Below is my code. 下面是我的代码。

public void takeInput(View view) {
        Intent intent = new Intent(this, WebViewActivity.class);
        EditText editText = (EditText) findViewById(R.id.editText);
        Integer vendorID = Integer.parseInt(editText.getText().toString());
        intent.putExtra(EXTRA_VENDORID, vendorID);
        startActivity(intent);

Above is the method that is called when an onscreen button is pressed OnClick. 上面是按下屏幕按钮OnClick时调用的方法。

The code from the next activity is below. 下一个活动的代码如下。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    String testURL2 = "www.google.com";
    WebView streetwiseSite = (WebView) this.findViewById(R.id.webView);
    streetwiseSite.getSettings().setJavaScriptEnabled(true);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String vendorID = extras.getString(MainActivity.EXTRA_VENDORID);
    Integer vendorID = Integer.parseInt(extras.getString(MainActivity.EXTRA_VENDORID));
    String targetURL = "www.something.org/purchase/";

Here is the XML code for my first activity. 这是我的第一个活动的XML代码。

<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"
    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=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/logo" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:text="MobilePay"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:hint="Vendor ID #"
        android:inputType="number" />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:onClick="takeInput"
        android:text="Pay" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="Enter Vendor ID and"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:text="Payment Amount to Pay"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Here is my XML code for the WebView activity, the second activity. 这是第二个活动WebView活动的XML代码。

<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"
    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.example.august.streetwise.WebViewActivity">


    <WebView
        android:id="@+id/webView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

Ideally this should take the user's input, parse it into the URL, and load that website. 理想情况下,这应该吸收用户的输入,将其解析为URL,然后加载该网站。 However, here are the errors I get from logcat. 但是,这是我从logcat得到的错误。

04-21 23:36:14.837 17904-17904/com.myname.app W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 17904 04-21 23:36:14.837 17904-17904 / com.myname.app W / BindingManager:无法调用确定的Visibility()-从未看到该pid的连接:17904

Any ideas? 有任何想法吗? I've been pulling my hair out over this. 我一直在为此扯头发。

WebView.loadUrl() does not add any protocol formatting to the beginning of the string. WebView.loadUrl()不会在字符串的开头添加任何协议格式。 Therefore, www.something.org does not automatically become http://www.something.org . 因此, www.something.org不会自动成为http://www.something.org

Without the protocol information at the beginning of the string, WebView.loadUrl() throws the decidedly unhelpful error W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid because www.something.org isn't a complete URL. 如果在字符串的开头没有协议信息,则WebView.loadUrl()会抛出绝对无济于事的错误W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid因为www.something.org并非完整的URL 。

You would think this behavior would be documented somewhere. 您会认为这种行为将在某处记录。 ;) ;)

change the following line 更改以下行

Intent intent = new Intent(this, WebViewActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
Integer vendorID = Integer.parseInt(editText.getText().toString());
 Bundle bn=new Bundle();
bn.putInt(EXTRA_VENDORID, vendorID);
intent.putExtras(bn);

startActivity(intent);

in second activity 在第二活动中

setContentView(R.layout.activity_web_view);
String testURL2 = "www.google.com";
WebView streetwiseSite =(WebView) this.findViewById(R.id.webView);
streetwiseSite.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int vendorID = extras.getInt(MainActivity.EXTRA_VENDORID);
String targetURL = "www.something.org/purchase/";

and for passing this vendorid to a url you should do something like this 为了将此供应商ID传递给URL,您应该执行以下操作

 String targetURL = "www.something.org/purchase&KEYWORD="+vendorID;

nb:KEYWORD should be the exact word which is used in the api side of the url which you are using nb:KEYWORD应该是您所使用的url的api端中使用的确切单词

try this to load url in webview 试试这个加载URL在WebView

web.setWebViewClient(new WebViewClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl(targetURL);

我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象的信息传递给另一个活动 - Pass object's info to another activity 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity 如何将一个活动的edittext字符串传递给另一个活动中的方法? - How to pass an edittext string from an activity to a method in another activity? 我如何通过 ArrayList<object> 从一个活动到另一个活动<div id="text_translate"><p>我想将 Object 的 ArrayList 从一个活动传递到另一个活动。</p><p> 这是示例代码:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么现在我将如何将 object 从这里传递给另一个活动。 <strong>但是</strong>,我可以使用<strong>Gson</strong>获得值。 通过将值转换为<strong>json</strong>并将其作为字符串传递给<strong>SharedPreference</strong>并从另一个活动中检索,然后使用<strong>类型</strong>从<strong>Json</strong>转换回来以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>传递值。 因为我在使用它时得到了 null 值。</p><p> 这是我试图使我的 Object 变量 Parceable 的代码:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> - How do I pass ArrayList<Object> from one Activity to another Activity 将 EditText 传递给新的 Activity - pass EditText to new Activity ListView到New Activity,然后使用Hashmap中的SQLite数据填充EditText - ListView to New Activity then Populate EditText's with SQLite data from Hashmap 如何将数据正确地从一项活动传递到另一项活动? - how can I pass data correctly from one activity to another? 如何将 Bitmap 对象从一个活动传递到另一个活动 - How can I pass a Bitmap object from one activity to another 如何使用图像路径从一个活动传递到另一个活动? - how do i pass image using image path from one activity to another? 如何将变量从一个活动传递到另一个活动? - How to pass variable from one activity to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM