如何在Android中更改按钮文本和功能?

如何在Android中更改按钮文本和功能?,android,android-intent,kotlin,Android,Android Intent,Kotlin,我是一个Android初学者。这就是我要做的。我有一个带有三个按钮的活动界面。第二个活动相同,但按钮文本和操作不同。与其在第一个活动中单击按钮时切换意图或活动,我可以对按钮进行编码以在单击时进行更改吗?这样我就不需要第二个相同的UI 这三个按钮是登录、注册和游览。当登录或游览被点击时,我确实希望他们启动不同的活动。但对于“注册”来说,用户界面是相同的,包含相同的按钮,但文本不同,并且会启动不同的意图。我的目标是消除这种相同的用户界面,只需在单击“注册”时更改第一个屏幕上的按钮 这是我当前的代码,

我是一个Android初学者。这就是我要做的。我有一个带有三个按钮的活动界面。第二个活动相同,但按钮文本和操作不同。与其在第一个活动中单击按钮时切换意图或活动,我可以对按钮进行编码以在单击时进行更改吗?这样我就不需要第二个相同的UI

这三个按钮是登录、注册和游览。当登录或游览被点击时,我确实希望他们启动不同的活动。但对于“注册”来说,用户界面是相同的,包含相同的按钮,但文本不同,并且会启动不同的意图。我的目标是消除这种相同的用户界面,只需在单击“注册”时更改第一个屏幕上的按钮

这是我当前的代码,它只需点击一下就可以启动新的意图。我不确定从哪里开始获得我想要的功能。感谢您的帮助。谢谢

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.content.Intent
import android.support.v4.content.ContextCompat.startActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun login(view: View) {
        val myIntent = Intent(this@MainActivity, LoginActivity::class.java)
        this@MainActivity.startActivity(myIntent)
    }

    fun signUpAs(view: View) {
        val myIntent = Intent(this@MainActivity, SignUpAsActivity::class.java)
        this@MainActivity.startActivity(myIntent)
    }

    fun tour(view: View) {
        val myIntent = Intent(this@MainActivity, TourActivity::class.java)
        this@MainActivity.startActivity(myIntent)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            val decorView = window.decorView
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        }
    }

}

我建议您保留登录代码,并在不同的活动或片段中注册(现在的方式)。如果您想消除UI复制,请考虑创建一个单独的布局(简单方法)或自定义视图(更先进的方法)与三个按钮。 这里有一个例子

res/layout/layout\u按钮\u菜单.xml

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

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button3" />
</LinearLayout>
<?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="wrap_content">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPassword" />

   <include layout="@layout/layout_buttons_menu" />
</LinearLayout>

我建议您保留登录代码,并在不同的活动或片段中注册(现在的方式)。如果您想消除UI复制,请考虑创建一个单独的布局(简单方法)或自定义视图(更先进的方法)与三个按钮。 这里有一个例子

res/layout/layout\u按钮\u菜单.xml

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

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button3" />
</LinearLayout>
<?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="wrap_content">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPassword" />

   <include layout="@layout/layout_buttons_menu" />
</LinearLayout>

在注册活动中,将布局设置为R.layout.activity\u main

setContentView(R.layout.activity_main)

GetRequired按钮并动态设置文本或任何其他必需属性

Button mButton=(Button)findViewById(R.id.mybutton);
mButton.setText("MyButton");

提示:您可以使用合成属性删除
SignUpAsActivity
中的findviewbyid:

,将layout设置为R.layout.activity\u main

setContentView(R.layout.activity_main)

GetRequired按钮并动态设置文本或任何其他必需属性

Button mButton=(Button)findViewById(R.id.mybutton);
mButton.setText("MyButton");

提示:您可以使用合成属性来去除findviewbyid:

谢谢。但是你为什么建议把活动分开?我不确定我的需要是否被理解。我需要两套独立的按钮。我需要第二组按钮仅在单击第一组按钮中的“注册”时显示。第一组和第二组的所有其他按钮将启动单独的活动。我认为消除几乎相同的活动是有意义的,因为只有按钮文本和它们启动的意图不同。这有意义吗?如果没有,请解释为什么我不应该这样做,为什么我应该坚持单独的活动。谢谢,嘿,安森伯林格。为了在屏幕上“交换”按钮,您可以使用两种带有按钮的布局。可以通过在可见/消失之间切换布局可见性来切换它们。但我最初的想法是将注册和登录UI保持在单独的片段/视图/活动中。虽然它看起来像是一个复制品(因为你提到UI非常相似),但它将帮助你在未来扩展应用程序OK很棒。谢谢你的解释。但是你为什么建议把活动分开?我不确定我的需要是否被理解。我需要两套独立的按钮。我需要第二组按钮仅在单击第一组按钮中的“注册”时显示。第一组和第二组的所有其他按钮将启动单独的活动。我认为消除几乎相同的活动是有意义的,因为只有按钮文本和它们启动的意图不同。这有意义吗?如果没有,请解释为什么我不应该这样做,为什么我应该坚持单独的活动。谢谢,嘿,安森伯林格。为了在屏幕上“交换”按钮,您可以使用两种带有按钮的布局。可以通过在可见/消失之间切换布局可见性来切换它们。但我最初的想法是将注册和登录UI保持在单独的片段/视图/活动中。虽然它看起来像是一个复制品(因为你提到UI非常相似),但它将帮助你在未来扩展应用程序OK很棒。谢谢你的解释。