简体   繁体   English

如何在java中的URL中间添加用户输入?

[英]How do I add user input in the middle of a URL in java?

I've been working on an Android program for a bit and I'm going crazy here... I'm trying to let the user enter a few numbers that would be put into the middle of a URL.我一直在研究一个 Android 程序,我在这里发疯了......我试图让用户输入一些将被放入 URL 中间的数字。 I need the code to read from a text box.我需要从文本框中读取代码。

Here's what I've got so far:这是我到目前为止所得到的:

public void browser(View view) 
{
    Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.website.com/(',R.id.isbn'));
    startActivity(browserIntent);
}

Doing this just adds the (',R.id.isbn'));这样做只是添加了 (',R.id.isbn')); to the URL.到网址。

How do I fix this?我该如何解决?

Thanks!谢谢!

Try it in this way.用这种方法试试。

EditText t= (EditText)findViewById(R.id.isbn);  
String st= t.getText().toString();
Uri.parse("http://www.website.com/"+st); 

Supposing that you are talking about Android EditText field, you could read it's value using editText.getText().toString() .假设您正在谈论 Android EditText 字段,您可以使用editText.getText().toString()读取它的值。 This is an example on how to use it in your application:这是有关如何在您的应用程序中使用它的示例:

@Override
public void onCreate(Bundle savedInstanceState) {
    //... init code cutted

    Button button = (Button)findViewById(R.id.button);
    EditText editText   = (EditText)findViewById(R.id.isbn);

    button.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                String isbnText = editText.getText().toString());
                // other code to open the new activity
            }
    });
}

Let your URL be like String url ="http://www.website.com/%1s/somevalue/%2s/etc+%3s";让你的 URL 像 String url ="http://www.website.com/%1s/somevalue/%2s/etc+%3s";

Call String.format(url,1,2,3);调用 String.format(url,1,2,3);

Hope this will solve your problem希望这能解决你的问题

The answer Anand posted is a simple solution to your problem. Anand 发布的答案是您问题的简单解决方案。 If you want the ISBN to be in the middle of the URL, you can just concatenate it like this:如果您希望 ISBN 位于 URL 的中间,您可以像这样连接它:

EditText t = (EditText) findViewById(R.id.isbn);  
String isbn = t.getText().toString();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.website.com/" + isbn + "/more-text-here"));
startActivity(browserIntent);

Read more about concatenating strings here:在此处阅读有关连接字符串的更多信息:

http://www.javatpoint.com/string-concatenation-in-java http://www.javatpoint.com/string-concatenation-in-java

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM