有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

按下后退按钮时禁用java Android按钮

我是Android编程新手,我对活动生命周期感到困惑。我读过几篇关于它的文章,但我仍然不知道如何让后退按钮工作

我有一个带有按钮的主活动,可以转到新活动,这很好,但是当我使用设备返回按钮时,当它返回到主活动时,按钮被禁用。我想这是因为我需要重新初始化主活动,但我不知道如何初始化。我试着恢复,但没有成功。以下是我的两项活动:

package com.rootsofempathy.recoveryjunior

import 安卓.content.Intent
import 安卓x.appcompat.app.AppCompatActivity
import 安卓.os.Bundle
import 安卓.widget.ImageButton

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button_gr = findViewById<ImageButton>(R.id.gr_btn)
        button_gr.setOnClickListener {
            println("Button Clicked")
            val activity2Intent = Intent(applicationContext, getting_ready::class.java)
            startActivity(activity2Intent)
        }

        val buttonOne = findViewById<ImageButton>(R.id.theme1_btn)
        buttonOne.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_One::class.java)
            startActivity(activity2Intent)
        }

        val buttonTwo = findViewById<ImageButton>(R.id.theme2_btn)
        buttonTwo.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Two::class.java)
            startActivity(activity2Intent)
        }

        val buttonThree = findViewById<ImageButton>(R.id.theme3_btn)
        buttonThree.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Three::class.java)
            startActivity(activity2Intent)
        }

        val buttonFour = findViewById<ImageButton>(R.id.theme4_btn)
        buttonFour.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Four::class.java)
            startActivity(activity2Intent)
        }

        val buttonFive = findViewById<ImageButton>(R.id.theme5_btn)
        buttonFive.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Five::class.java)
            startActivity(activity2Intent)
        }

        val buttonSix = findViewById<ImageButton>(R.id.theme6_btn)
        buttonSix.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Six::class.java)
            startActivity(activity2Intent)
        }

        val buttonSeven = findViewById<ImageButton>(R.id.theme7_btn)
        buttonSeven.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Seven::class.java)
            startActivity(activity2Intent)
        }

        val buttonEight = findViewById<ImageButton>(R.id.theme8_btn)
        buttonEight.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Eight::class.java)
            startActivity(activity2Intent)
        }

        val buttonNine = findViewById<ImageButton>(R.id.theme9_btn)
        buttonNine.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Nine::class.java)
            startActivity(activity2Intent)
        }

        val buttonTen = findViewById<ImageButton>(R.id.theme10_btn)
        buttonTen.setOnClickListener {
            val activity2Intent = Intent(applicationContext, Theme_Ten::class.java)
            startActivity(activity2Intent)
        }
    }
}
class Theme_One : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_theme_one)

        val config = PdfActivityConfiguration.Builder(this@Theme_One).build()

        val assetFile = Uri.parse("file:///安卓_asset/theme_one.pdf")
        PdfActivity.showDocument(this@Theme_One, assetFile, null, config)

    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.rootsofempathy.recoveryjunior">

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:roundIcon="@mipmap/ic_launcher_round"
        安卓:supportsRtl="true"
        安卓:theme="@style/Theme.RecoveryJunior">

        <activity
            安卓:name=".Theme_Ten"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Nine"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Eight"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Seven"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Six"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Five"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Four"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Three"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_Two"
            安卓:exported="true" />
        <activity
            安卓:name=".Theme_One"
            安卓:exported="true" />
        <activity
            安卓:name=".MainActivity"
            安卓:exported="true">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            安卓:name="com.pspdfkit.ui.PdfActivity"
            安卓:theme="@style/RecoveryJunior.PSPDFKitTheme"
            安卓:windowSoftInputMode="adjustNothing" />
        <activity
            安卓:name=".getting_ready"
            安卓:theme="@style/Theme.AppCompat.Light.NoActionBar"
            安卓:windowSoftInputMode="adjustNothing" />

        <meta-data
            安卓:name="pspdfkit_license_key"
            安卓:value="" />
    </application>

</manifest>

如果有人能给我举一个清晰的例子,说明如何正确地使后退按钮重新启动主要的活动按钮,我将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    您可以使用两种方法解决此问题:

    #1启动PdfActivity后调用finish()

    class Theme_One : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_theme_one)
    
            val config = PdfActivityConfiguration.Builder(this@Theme_One).build()
    
            val assetFile = Uri.parse("file:///android_asset/theme_one.pdf")
            PdfActivity.showDocument(this@Theme_One, assetFile, null, config)
            finish()
        }
    }
    

    #2根据您当前的代码,您可以直接启动PdfActivity并在点击按钮时跳过Theme_one活动

    buttonOne.setOnClickListener {
            
            val config = PdfActivityConfiguration.Builder(this).build()
    
            val assetFile = Uri.parse("file:///android_asset/theme_one.pdf")
            PdfActivity.showDocument(this@Theme_One, assetFile, null, config)
    
        }