在主活动中建立多个button对应不同页面及intent语句应用

首先新建Activity,建立button
布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/button_website"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="website" />
</LinearLayout>

其次建立跳转Activity,SecondActivity&ThirdActivity

//secondactivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button_website"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="button_website" />
</LinearLayout>


//thirdactivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button_3" />
</LinearLayout>

接下来为各个按钮添加监听及响应事件

//FirstActivity
public class FirstActivity extends AppCompatActivity {
    public static final String TAG="FirstActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button startSecondActivity=(Button) findViewById(R.id.button_1);
        Button startThridActivity=(Button) findViewById(R.id.button_website);
        startSecondActivity.setOnClickListener(new View.OnClickListener(){
        //此处INTENT用于向下一界面传递信息
            @Override
            public void onClick(View v) {
                String data="hello world";
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("extra_data",data);
                startActivity(intent);
            }
        });
        startThridActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, ThridActivity.class);
                startActivity(intent);
            }
        });
    }
}

//SecondActivity
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Intent intent=getIntent();
        String data=intent.getStringExtra("extra_data");
        Log.e("SecondActivity",data);
        Button button2=(Button) findViewById(R.id.button_website);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //这里使用隐式intent启动浏览器展示网页
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
    }
}


//ThirdActivity
public class ThridActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thrid_activity);
        Button button=(Button) findViewById(R.id.button_3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ThridActivity.this,FirstActivity.class);
                startActivity(intent);
            }
        });
    }
}

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读