简体   繁体   中英

How do I change button text and function in Android?

I am an Android beginner. Here is what I am trying to do. I have an activity UI with three buttons. 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.

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. My goal is to eliminate this identical UI and just have the buttons change on the first screen when "Sign Up" is clicked.

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.

Here is an example.

res/layout/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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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