<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.faraz.button_example.MainActivity"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to includehelp.com"
tools:layout_editor_absoluteX="110dp"
tools:layout_editor_absoluteY="215dp" />
</android.support.constraint.ConstraintLayout>
2) File: MainActivity.java 2)檔案:MainActivity.java package com.example.faraz.button_example;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.includehelp.com"));
startActivity(browserIntent);
}
});
}
}
Output 輸出量 After executing your code on your virtual device, you get following output. As you click on the button you will be directed to includehelp.com page.
在虛拟裝置上執行代碼後,将獲得以下輸出。 當您單擊按鈕時,您将被定向到includehelp.com頁面。
翻譯自: https://www.includehelp.com/android/button-example-code.aspx