【发布时间】:2021-12-17 10:41:17
【问题描述】:
我正在尝试将链接添加到我在片段上创建的按钮。这是我目前所拥有的。如果它在主要活动上,我可以让按钮工作,但我不知道从那里做什么。
java
public class RideSharesFragment extends Fragment {
public RideSharesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_ride_shares, container, false);
}
public void Uber(View view){
Intent browserIntent = new Intent(
Intent.ACTION_VIEW, Uri.parse("https://m.uber.com/")
);
startActivity(browserIntent);
}
}
XML
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Ride_shares"
android:textColor="@color/black"
android:textSize="32sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_`enter code here`constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lyft"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.203" />
<Button
android:id="@+id/Uber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uber"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】: