Android 实现按钮 跳转到某网页

这个是我在刚开始学习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的都会写出这个程序了,哈哈哈哈。    加油!!

猜你喜欢

转载自blog.csdn.net/z1web/article/details/84325897