Próbując zrozumieć Javę i Androida, potrzebna jest pomoc w prostym zadaniu otwierania przeglądarki użytkownika po kliknięciu przycisku.
Robiłem tutoriale przez ostatnie dwa dni, ale może to pomóc, jeśli po prostu się tym zajmę i otrzymam informację zwrotną. z góry dzięki za wszelką pomoc.
Main.xml:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgimage2">
>
<Button
android:id="@+id/goButton"
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_x="80px"
android:layout_y="21px"
>
</AbsoluteLayout>
GetURL.java:
package com.patriotsar;
import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;
String url = "http://www.yahoo.com";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);
public class patriosar extends Activity {
private Button goButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
try {
// Start the activity
startActivity(i);
} catch (ActivityNotFoundException e) {
// Raise on activity not found
Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
}
}
});
}
}
2 odpowiedzi
Jest blisko, ale kilka rzeczy jest w niewłaściwym miejscu lub brakuje. Poniższy kod działa - starałem się dokonać minimalnych niezbędnych zmian. Możesz załadować obie wersje do czegoś takiego jak WinMerge, aby zobaczyć dokładnie, co się zmieniło.
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgimage2"
>
<Button
android:id="@+id/goButton"
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_x="80px"
android:layout_y="21px"
></Button>
</LinearLayout>
GetURL.java:
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class GetURL extends Activity {
private Button goButton;
String url = "http://www.yahoo.com";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
Context context = this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
goButton = (Button)findViewById(R.id.goButton);
goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
try {
// Start the activity
i.setData(u);
startActivity(i);
} catch (ActivityNotFoundException e) {
// Raise on activity not found
Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
}
}
});
}
}
(Potrzebujesz również pliku bgimage2.png
w /res/drawable/
i ciągu start
w /res/values/strings.xml
, oczywiście).
Aby uprościć, możesz zrobić
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com" )); startActivity(zamiar);
Podobne pytania
Nowe pytania
java
Java to język programowania wysokiego poziomu. Użyj tego tagu, jeśli masz problemy z używaniem lub zrozumieniem samego języka. Ten tag jest rzadko używany samodzielnie i jest najczęściej używany w połączeniu z [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] i [maven].
AbsoluteLayout
. Ta klasa kontenera jest przestarzała. Użyj czegoś innego.