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