Android 實現按鈕 跳轉到某網頁
阿新 • • 發佈:2019-02-02
這個是我在剛開始學習Android的時候,我同學要做一個手機端的控制,他說我的東西都用 java web寫好了,你只要給我寫一個跳轉按鈕即可,其實很簡單只是簡單地按鈕點選事件和Intent跳轉。但是畢竟是第一次幫別人做東西還覺得挺有意義的,我就記下來了。
演示:
主要就是實現這個效果:下面我介紹一下程式碼部分
主程式結構:
可以看到我隱藏了標題欄,並且修改了程式的圖示和名稱,讓它看起來更加美觀。這個主要修改了AndroidManifest
實現程式碼如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.textview"> <application android:allowBackup="true" android:icon="@drawable/bb1" //程式的圖示 android:label="@string/app_name" // 程式的名稱 android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> // 設定無標題欄 <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
然後因為是要設定無標題欄所以style也要修改:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> //設定無標題欄 <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
然後就是修改主介面,可以看到主介面有一個好看的背景和一個修改過後的按鈕,主要修改layout裡的程式碼。
主要實現如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" //垂直襬放 android:background="@drawable/p1" //好看的背景圖片 android:gravity="center" // 居中 android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/but1" android:text="@string/but_1" //設定按鈕上的文字 android:background="@drawable/butstyles" //設定按鈕的漸變色和圓角形狀 android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然後那個butstyles那個檔案如下:
<!-- android:shape指定形狀型別,預設為rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- solid指定形狀的填充色,只有android:color一個屬性 -->
<solid android:color="#4097e6" />
<!-- padding設定內容區域離邊界的間距 -->
<padding
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp" />
<!--漸變設定-->
<gradient
android:endColor="#4097e6"
android:startColor="#FFFFFF"
android:angle="270"
android:type="linear" />
<!-- corners設定圓角,只適用於rectangle
數值較大時,就變成了弧邊形狀例如>=200時
-->
<corners android:radius="15dp" />
<!-- stroke設定描邊 -->
<stroke
android:width="2dp"
android:color="#ff8900"
android:dashGap="4dp"
android:dashWidth="4dp" />
</shape>
然後實現了跳轉網頁就很簡單了,就是一個簡單的點選事件在MainActivity中,程式碼如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.but1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("http://www.baidu.com"); //設定跳轉的網站
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
這個就是這個程式的所有程式碼了,是不是超級簡單。看了我的教程我覺得沒學過android的都會寫出這個程式了,哈哈哈哈。 加油!!