簡體   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