簡體   English   中英

如何為我的應用程序創建另一個頁面的按鈕

[英]How do i create a button to another page for my app

我只是想知道如何在頁面上創建一個按鈕,以便將其轉到應用程序的另一個頁面。 我是一個初學者,因此,如果您能解釋一下這一切的工作原理和發展方向,將會很有幫助。 PS我正在使用android studio,如果這有所作為,這是我到目前為止在fragment_main.xml中的代碼。 我尚未在.java中輸入任何代碼

<TextView android:text="@string/hello_world"
    android:id="@id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ImageButton
    android:id="@+id/firstbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/homebutton"
    android:layout_below="@+id/text"/>

您可以在Java中動態聲明視圖和對象,然后將按鈕從一個片段傳遞到另一個片段(或從Activity到Activity,這取決於您的應用程序)。

要使用按鈕聲明相對布局,例如:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;

public class JavaLayoutActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button myButton = new Button(this);
    RelativeLayout myLayout = new RelativeLayout(this);
    myLayout.addView(myButton);
    setContentView(myLayout);
}  

這不會設置屬性或任何內容,我只是將其用作概念證明。

XML使用戶界面設計變得容易,因為您不必在代碼中進行管理,但這是一種例外。 如果需要動態接口對象,則需要使用Java。

而不是動態創建視圖,您應該在活動中獲取視圖

ImageButton button = (ImageButton) findViewById(R.id.firstButton)

並將一個onClick偵聽器分配給ID

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // start new Activity here
    }
});

您也可以在xml中執行此操作:

<ImageButton
    android:id="@+id/firstbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/homebutton"
    android:onClick="sendMessage"
    android:layout_below="@+id/text"/>

使用這樣的配置,您應該在活動中添加方法:

public void sendMessage(View view) {
    // start another activity here
}

android有兩種可用的方法,您可以使用它們從一個Activity轉到另一個Activity。

1.使用button.setOnClickListener()

xml文件中創建一個按鈕。

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

現在為.class文件中的按鈕設置事件監聽器

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //set the event you want to perform when button is clicked
        //you can go to another activity in your app by creating Intent
        Intent intent = new Intent(getApplicationContext, Activity2.class);
        startActivity(intent);
    }
});

2.使用<android:onClick="goNext">

onClick用作在xml文件中創建的按鈕的屬性。

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />

現在,在您的.class文件中,為該按鈕定義一個事件,

goNext() {
    Intent intent = new Intent(getApplicationContext, Activity2.class);
    startActivity(intent);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM