2017-06-01 66 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

相關問題