Я хочу создать EditText, пользователь которого будет вводить URL сайта как адресную строку. Когда кнопка нажата, он должен открыть предпочтительный браузер с телефона с указанным URL в EditText. Я использовал тот же код, который отлично работает с некоторыми кнопками и элементами ListView для кнопки Go, но он сохраняет, например, No apps can perform this action.
файл макета:
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/url_hint"
android:ems="12"
android:inputType="textWebEditText"
app:layout_constraintStart_toEndOf="@+id/textView26"
app:layout_constraintTop_toBottomOf="@+id/textView25" />
<Button
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:backgroundTint="@color/btn_color"
android:onClick="goPrefSite"
android:text="@string/go"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editText2"
app:layout_constraintTop_toBottomOf="@+id/textView25" />
java код:
import android.net.Uri;
import android.widget.EditText;
import android.view.View;
import android.content.Intent;
public void goPrefSite(View v){
//reference to EditText
EditText address = (EditText) findViewById(R.id.editText2);
//store address in String
String myAddress = address.getText().toString();
//create intent to open browser with address from the WebAddress class
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(myAddress));
//create and start the chooser
Intent chooser = Intent.createChooser(intent, "Open with...");
startActivity(chooser);
}