【问题标题】:is there a way that i can access a url on button click without opening the website in android?有没有一种方法可以让我在不打开 android 网站的情况下点击按钮访问 url?
【发布时间】:2021-09-16 16:59:23
【问题描述】:

例如,我制作了一个 Nodemcu 服务器,主网页上有 3 个链接(192.168.18.100)。 我想尝试的是,当我单击 android 按钮时,无需打开任何新活动即可访问此链接,即我想留在主要活动上。 到目前为止,这是我的代码,但不是我想要的。

public class MainActivity extends AppCompatActivity {

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

    Button on = (Button) findViewById(R.id.button);
    Button off =(Button) findViewById(R.id.button2);
    Button pink = (Button) findViewById(R.id.button3);

    on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_SEND, uri);
            startActivity(intent);
        }
    });
    off.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
    pink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
}

}

【问题讨论】:

    标签: java android html


    【解决方案1】:

    您可以尝试在 MainActivity 上实现 WebView。当您单击任何按钮时,使用所需的 url 加载 WebView。

    你可以在这个link中找到实现WebView的方法

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2011-10-17
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2020-06-20
    • 2014-07-18
    相关资源
    最近更新 更多