效果
代码
关键代码
public void onClick(View view)
{
EditText url = findViewById(R.id.neturl);
Intent cn = new Intent(Intent.ACTION_VIEW, Uri.parse("https://"+url.getText().toString()));
startActivity(cn);
}
完整代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/neturl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="0"
android:text="浏览器搜索" />
</LinearLayout>
package com.example.application;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
if(button != null){
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText url = findViewById(R.id.neturl);
Intent cn = new Intent(Intent.ACTION_VIEW, Uri.parse("https://"+url.getText().toString()));
startActivity(cn);
}
});
}
}
}
关注
笔者 - jxd
微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O