繁体   English   中英

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

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

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

这三个按钮是“登录”,“注册”和“游览”。 当我单击“登录”或“游览”时,我确实希望他们启动其他活动。 但是对于“ SignUp”,这是UI相同的地方,其中包含相同的按钮但不同的文本,并且将启动不同的意图。 我的目标是消除相同的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_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 /布局/ 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标签将允许您重用UI组件。 官方文档在这里

在您的活动中,您可以通过常用方式访问这些按钮

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

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

的setContentView(R.layout.activity_main)

获取所需按钮并动态设置文本或任何其他必需属性。

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

提示:您可以使用合成属性来摆脱findviewbyid: https ://kotlinlang.org/docs/tutorials/android-plugin.html

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM