android studio实现界面切换

实现:主界面与次界面来回切换

源码如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别"
        android:textSize="23dp"
        />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btnMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/btnWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

    <Button
        android:id="@+id/btnpost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>

    <Button
        android:id="@+id/btnswitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="切换界面"/>


</LinearLayout>
MainActivity.java
package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//    Button mybtn ,btn2;
    RadioGroup radgroup ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radgroup = (RadioGroup)findViewById(R.id.radioGroup);
        radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radbtn = (RadioButton)findViewById(checkedId);
                Toast.makeText(getApplicationContext(),"按钮值发生改变,你选了"+radbtn.getText(),Toast.LENGTH_SHORT).show();
            }
        });

        Button btnchange = (Button) findViewById(R.id.btnpost);
        btnchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<radgroup.getChildCount();i++)
                {
                    RadioButton rd = (RadioButton) radgroup.getChildAt(i);
                    if(rd.isChecked()){
                        Toast.makeText(getApplicationContext(),"点击提交按钮,你选择的是:" + rd.getText(),Toast.LENGTH_LONG).show();;
                        break;
                    }

                }
            }
        });

        Button btnswitch = (Button) findViewById(R.id.btnswitch);
        btnswitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,testActivity.class);
                startActivity(intent);
            }
        });
//        mybtn = (Button) findViewById(R.id.button);
//        mybtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if(mybtn.getText().equals("btn_test"))
//                {
//                    mybtn.setText("hello");
//                    Log.d("mybtn","click 1");
//                }
//                else {
//                    mybtn.setText("btn_test");
//                    Log.d("mybtn","click 2");
//                }
//
//            }
//        });


//        TextView mytxt = findViewById(R.id.txtOne);
//        String s1 = "<font color='blue'><b>百度一下,你就知道~:</b></font><br>";
//        s1 += "<a href = 'http://www.baidu.com'>百度</a>";
//        mytxt.setText(Html.fromHtml(s1));
//        mytxt.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".testActivity">

    <Button
        android:id="@+id/button_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="alen"
        tools:layout_editor_absoluteX="116dp"
        tools:layout_editor_absoluteY="174dp" />
    <Button
        android:id="@+id/btn_ret"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回主界面"
        tools:layout_editor_absoluteX="116dp"
        tools:layout_editor_absoluteY="174dp" />
</LinearLayout>

testActivity.java

package com.example.test2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class testActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        Button btnret=findViewById(R.id.btn_ret);
        btnret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //方法1
                //直接finish();
                //finish(); // ok

                //方法2
                Intent intent = new Intent(testActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Test2">
        <activity
            android:name=".testActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • android:exported 属性指定了活动是否能够被其他应用程序组件调用。在这里,testActivity 是不允许被其他应用调用的(android:exported="false"),而 MainActivity 允许被其他应用调用(android:exported="true")。
  • <intent-filter> 元素定义了主活动,即启动应用程序时首先打开的活动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alen.Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值