简体   繁体   English

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

[英]How do I change button text and function in Android?

I am an Android beginner. 我是Android初学者。 Here is what I am trying to do. 这是我想要做的。 I have an activity UI with three buttons. 我有一个带有三个按钮的活动UI。 A second activity is identical however the buttons text and actions are different. 第二个活动是相同的,但是按钮的文本和操作是不同的。 Rather than have it switch intents or activities when buttons are clicked on the the first activity, can I code the buttons to change when clicked? 在第一个活动上单击按钮时,它没有切换意图或活动,而是可以对按钮进行编码以便在单击时进行更改吗? This way I wouldn't need a second identical UI. 这样,我就不需要第二个相同的UI。

The three buttons are Login, SignUp and Tour. 这三个按钮是“登录”,“注册”和“游览”。 When Login or Tour are clicked I do indeed want them to launch different activities. 当我单击“登录”或“游览”时,我确实希望他们启动其他活动。 But for "SignUp" this is where the UI would be identical containing the same buttons but different text and will launch different intents. 但是对于“ SignUp”,这是UI相同的地方,其中包含相同的按钮但不同的文本,并且将启动不同的意图。 My goal is to eliminate this identical UI and just have the buttons change on the first screen when "Sign Up" is clicked. 我的目标是消除相同的UI,只需在单击“注册”时在第一个屏幕上更改按钮。

Here is my current code which just launches new intents on click. 这是我当前的代码,它可以在点击时启动新的意图。 I am not sure where to start to get the functionality I want. 我不确定从哪里开始获得我想要的功能。 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

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)
        }
    }

}

I'd suggest you to keep your code for login and sign up in different Activities or Fragments (the way it is now). 我建议您保留代码以进行登录,并在不同的“活动”或“片段”中进行注册(现在是这样)。 If you want to eliminate UI duplication please consider creating a separate layout (simple approach) or custom view (more advanced approach) with three buttons. 如果要消除UI重复,请考虑使用三个按钮创建单独的布局(简单方法)或自定义视图(更高级的方法)。

Here is an example. 这是一个例子。

res/layout/layout_buttons_menu.xml RES /布局/ layout_buttons_menu.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>

res/layout/activity_login.xml RES /布局/ activity_login.xml

<?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>

include tag will allow you to reuse UI components. include标签将允许您重用UI组件。 Official documentation here . 官方文档在这里

In your activity you can access these buttons in a common way 在您的活动中,您可以通过常用方式访问这些按钮

 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login)
        val button1 = findViewById(R.id.button1) as Button
 }

In SignUpAsActivity set layout to R.layout.activity_main. SignUpAsActivity将布局设置为R.layout.activity_main。

setContentView(R.layout.activity_main) 的setContentView(R.layout.activity_main)

Get required button and set the text or any other required attribute dynamically. 获取所需按钮并动态设置文本或任何其他必需属性。

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

Tip: You can use synthetic properties to get rid of findviewbyid : https://kotlinlang.org/docs/tutorials/android-plugin.html 提示:您可以使用合成属性来摆脱findviewbyid: https ://kotlinlang.org/docs/tutorials/android-plugin.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android上将向上操作图标更改为带有文本的按钮? - How do I change the up action icon into button with text on Android? Android-如何将按钮文本更改为arraylist项目 - Android - How do I change button text to arraylist item 如何更改按钮的文本颜色? - How do I change the text color of a Button? 更改按钮文字并在Android Studio中执行相应的onClick功能 - Change button text and do corresponding onClick function in android studio 如何更改android中按钮的样式? - how do I change the style of a button in android? 如何在Android中使用onclick更改按钮? - How do I change a button with onclick in android? 如何更改android中按钮的背景? - How do I change the background of a button in android? 当单击其他按钮,但仍具有相同功能时,如何更改按钮文本? 安卓系统 - How can I change a buttons text when a different button is clicked and still have it function the same? Android 如何以编程方式更改 Android 中 Material Component Text Button 的样式? - How do I programmatically change the style of a Material Component Text Button in Android? 在android studio中按下按钮后,如何更改其文本? - How do I change my button's Text after it is pressed in android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM