2017-06-01 68 views
0

我是一名Android初学者。这是我想要做的。我有一个有三个按钮的活动UI。第二个活动是相同的,但按钮的文字和动作是不同的。当点击第一个活动的按钮时,而不是让它切换意图或活动,我可以编码按钮以单击时更改吗?这样我就不需要第二个相同的用户界面。如何更改Android中的按钮文字和功能?

这三个按钮是登录,注册和游览。点击登录或导览时,我确实希望他们发起不同的活动。但对于“SignUp”来说,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([email protected], LoginActivity::class.java) 
     [email protected](myIntent) 
    } 

    fun signUpAs(view: View) { 
     val myIntent = Intent([email protected], SignUpAsActivity::class.java) 
     [email protected](myIntent) 
    } 

    fun tour(view: View) { 
     val myIntent = Intent([email protected], TourActivity::class.java) 
     [email protected](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) 
     } 
    } 

} 

回答

0

我建议你保持你的代码登录和注册在不同的活动或片段(现在的方式)。如果您想消除用户界面重复,请考虑使用三个按钮创建单独的布局(简单方法)或自定义视图(更高级的方法)。

这里是一个例子。

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> 

包括标签可以让你重用UI组件。官方文档here

在你的活动,你可以在一个共同的方式

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

感谢。但为什么你建议保持单独的活动?我不确定我的需求是否被理解。我需要有两套独立的按钮。我需要第二组按钮仅在点击第一组“注册”时显示。第一套和第二套的所有其他按钮将启动单独的活动。我认为消除几乎相同的活动是有意义的,因为只有他们发布的按钮文本和意图不同。那有意义吗?如果不是,请解释为什么我不应该这样做,为什么我应该坚持单独的活动。谢谢。 –

+0

嘿@AnsonBolinger。为了在屏幕上“交换”按钮,您可以使用两个带按钮的布局。您可以通过在VISIBLE/GONE之间切换布局可见性来切换它们。 但我最初的想法是将SignUp和登录界面保留在单独的Fragments/Views/Activitirs中。虽然它可能看起来像重复(因为你提到的UI非常相似),但它会帮助你在未来扩展应用程序 –

+0

好吧。感谢您的解释。 –

0

访问这些按钮在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

相关问题