【问题标题】:How do i create a button to another page for my app如何为我的应用程序创建另一个页面的按钮
【发布时间】:2015-04-14 14:27:40
【问题描述】:

我只是想知道如何在我的页面上制作一个按钮,以便它可以转到我的应用程序的另一个页面。我是一个初学者,所以如果你能解释它是如何工作的以及它的去向,那将会有很大帮助。 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 中声明按钮并在 Java 中设置位置,这样你就可以将它从一个片段传递到另一个片段。
  • 您需要开始新页面或新活动吗??

标签: java android xml button


【解决方案1】:

您可以在 Java 中动态声明视图和对象,然后将按钮从片段传递到片段(或 Activity 到 Activity,具体取决于您的应用)。

用Button声明一个Relative Layout,例如:

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。

【讨论】:

  • 你能解释一下把这段代码放在哪里吗,因为在我的主要活动的java中它有很多片段,我不知道把它放在哪里。还有我如何链接到其他页面。对不起,如果我问了明显的问题
  • 说实话,我正在工作,现在没有时间编写整个应用程序。如果你不知道 onCreate 方法是什么,你需要看一本书或者做更多的互联网研究。您刚刚提出的问题很容易通过上述任何一种方式来回答。
【解决方案2】:

您应该在活动中获取视图,而不是动态创建视图

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

并为 id 分配一个 onClick 侦听器

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
}

【讨论】:

    【解决方案3】:

    在 android 中有两种可用的方法,您可以使用它们从一个 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.使用&lt;android:onClick="goNext"&gt;

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 2016-08-03
      相关资源
      最近更新 更多