文本到url参数android app

时间:2018-02-23 12:09:01

标签: android url text parameters

我尝试使用应用来查询我公司的订单。这是布局: https://ibb.co/mh8EOH

这是我的代码:

#public class Orders extends AppCompatActivity {
    EditText query; // declare here

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_Orders);

        Button btn = (Button)findViewById(R.id.button);

        EditText et = (EditText)findViewById(R.id.editTextId1);
        String query = et.getText().toString();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), Webview.class);
                i.putExtra("url", "https://mysite/query?order="+query);
                startActivity(i);
            }
        });

    }
}

所以......在android studio中显示“Variale'查询'是从内部类中访问的,需要被声明为final”

怎么了?我想使用输入文本将参数传递给url。

2 个答案:

答案 0 :(得分:0)

只需将您的字符串query变量声明为final String query;

或像这样使其全球化

public class Orders extends AppCompatActivity {
    EditText et; 
    String url="";
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_Orders);

         btn = (Button)findViewById(R.id.button);

        et  = (EditText)findViewById(R.id.editTextId1);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                url = et.getText().toString();

                if(!TextUtils.isEmpty(url)){
                    Intent i = new Intent(getApplicationContext(), Webview.class);
                    i.putExtra("url", "https://mysite/query?order="+url);
                    startActivity(i);
                    }

            }
        });

    }

答案 1 :(得分:0)

因此,您需要在edittext内获得onclick listener值,如下所示: -

        et  = (EditText)findViewById(R.id.editTextId1);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String query = et.getText().toString();
                if(!query.isEmpty()) { 
                     Intent i = new Intent(getApplicationContext(), Webview.class);
                     i.putExtra("url", "https://mysite/query?order="+url);
                     startActivity(i);
                }
            }
        });