Android 不显示字符串连接
我输入的字符串的串联有问题 这是一节课:Android 不显示字符串连接,android,webview,concatenation,string-concatenation,Android,Webview,Concatenation,String Concatenation,我输入的字符串的串联有问题 这是一节课: package com.isma.multisitesearch.Siti; import com.isma.multisitesearch.R; import com.isma.multisitesearch.Webviews.GoogleWebView; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import andr
package com.isma.multisitesearch.Siti;
import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Webviews.GoogleWebView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Google extends Activity implements OnClickListener {
private String TAG_ACT = "Caricamento ricerca";
public EditText googletext;
private Button googlebutton;
private String googleurl = "https://www.google.it/search?q=";
public static String newgoogleurl;
public String space = " ";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google);
googletext = (EditText) findViewById(R.id.googletext);
googlebutton = (Button) findViewById(R.id.googlebutton);
googlebutton.setOnClickListener(this);
newgoogleurl = googleurl + googletext.getText().toString();
newgoogleurl.replaceAll(space, "%20");
System.out.println(newgoogleurl);
}
@Override
public void onClick(View v) {
Log.v(TAG_ACT, "in corso");
Intent intent = new Intent(Google.this, GoogleWebView.class);
startActivity(intent);
}
}
这是WebView
,我想获得连接:
package com.isma.multisitesearch.Webviews;
import com.isma.multisitesearch.R;
import com.isma.multisitesearch.Siti.Google;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class GoogleWebView extends Activity {
private WebView googlewebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlewebview);
googlewebview = (WebView) findViewById(R.id.googlewebview);
googlewebview.getSettings().setJavaScriptEnabled(true);
googlewebview.getSettings().setLoadsImagesAutomatically(true);
googlewebview.setWebViewClient(new WebViewClient());
googlewebview.loadUrl(Google.newgoogleurl);
}
}
该应用程序运行良好,但当我在EditText
中编写搜索文本时,google but I会进入主页,您可以在主页上输入搜索,而不是已经输入搜索文本的url
我希望你能帮助我,在这之后我就可以运行这个应用了。注意
newgoogleurl.replaceAll(space, "%20");
无效,您需要编写以下代码:
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
请看这里:
请尝试以下代码:
String url;
url = googleurl+googletext.getText().toString();
if( url.contains(" ")){
newgoogleurl=url.replaceAll(" ","%20");
}else{
newgoogleurl=url;
}
这也包含验证部分。字符串是不可变的。这意味着您不能修改String的实例,每次需要修改它时都需要创建一个新实例 在您的情况下,
replaceAll
不会修改您调用它的字符串,而是返回一个新字符串,并应用您的修改
这就是为什么需要影响字符串引用的返回值
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
此外,您应该阅读replaceAll
的文档,因为我认为它不符合您的要求。您可能想使用替换尝试以下方法:
@Override
public void onClick(View v) {
newgoogleurl = googleurl + googletext.getText().toString();
newgoogleurl = newgoogleurl.replaceAll(space, "%20");
Log.v(TAG_ACT, "in corso");
Intent intent = new Intent(Google.this, GoogleWebView.class);
startActivity(intent);
}
谢谢你的留言,我更正了,但没有任何效果,仍然没有连接字符串。你是最棒的,它为我找到了我想要的。非常感谢,我不能把一个+1的名声放在你的名声上,因为我的RP20小于15,但你应得的20:你怎么做?我是新来的