【问题标题】:Android Button to open xml layoutAndroid 按钮打开 xml 布局
【发布时间】:2011-08-19 23:00:35
【问题描述】:

您好,我是 android 开发的新手,想创建一个包含三个页面和一个主页的应用程序,主页上带有导航到不同页面的按钮。我已经创建了不同的 xml 页面,我相信我必须创建某种 onclick 东西,但我不确定。

我需要什么 java 代码,我还需要添加什么到普通按钮 xml 等。谁能告诉我一个例子来实现我的图片工作

这是我的按钮之一

<Button
  android:id="@+id/information"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textColor="#fff"
  android:background="@drawable/custombutton"
  android:text="Information  >"
  android:textSize="18dp"
  android:layout_marginBottom="2dp"
  />

这是我唯一拥有的 java 文件(我是添加还是新建一个)

package com.techie.layout;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import reading.festival.guide.R;

public class LayoutPractice extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//makes full screen and takes away title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//

setContentView(R.layout.linear);
}

}

我的其他xml布局叫sitemap、information和lineup,原来的xml叫linear。

我希望我已经提供了足够的信息来帮助我,我相信这很容易而且我很愚蠢

干杯

【问题讨论】:

    标签: android xml button navigation switch-statement


    【解决方案1】:

    最简单的方法是使用android:onClick 属性在您的xml 中定义点击处理程序:

    <Button
      android:id="@+id/information"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textColor="#fff"
      android:background="@drawable/custombutton"
      android:text="Information  >"
      android:textSize="18dp"
      android:layout_marginBottom="2dp"
      android:onClick="myClickHandler"
      />
    

    通过添加它,您是在告诉 Android 您的 Activity 中的函数名称,以便在您单击按钮时调用该函数。但是,此功能必须遵循一些规则。它的返回类型必须为 void,只接受一个参数,即视图。继续使用上面的示例,您需要在 Activity 中添加以下内容,即:

    public void myClickHandler(View v){
      //handle the click here
    }
    

    您可能需要为站点地图、信息和 linuep 布局创建单独的活动。不要忘记在您的 manifest.xml 中声明这些新活动。

    假设您创建了一个名为InformationActivity。要从我们的示例按钮单击启动它,您可以这样做:

     public void myClickHandler(View v){
           startActivity(new Intent(this, InformationActivity.class);
        }
    

    有关更多详细信息,请阅读 Android dev guide 中的活动和 UI。

    【讨论】:

    • 这听起来不错,可能是我想要的,但是你能不能说得非常慢,并且尽可能多地提供信息,就像你刚刚让我大吃一惊一样,但是谢谢(例如我把东西放在哪里,即文件等)
    • 我添加了更多细节和指向 android 开发指南的指针。您应该通读开发指南以了解所有基本概念。你可能也想买一本 Android 书。我很乐意回答您可能提出的具体问题。
    • 非常感谢您的帮助和非常快速的回复,您能告诉我在哪里创建这些活动等我仍然不确定将这些代码的 sn-ps 放在哪里
    • 每个 Activity 都是一个单独的类,就像您问题中的 LayoutPractice 一样。您可以将它们放在与 LayoutPractice 相同的位置。点击处理函数可以在你的 Activity 类中的任何地方使用。
    【解决方案2】:

    因此,当您说“页面”时,您的意思是活动。您需要为每个布局文件创建一个活动。

    阅读材料: http://developer.android.com/guide/topics/fundamentals/activities.html

    【讨论】:

      【解决方案3】:

      您应该将按钮对象添加到您的代码中并向其中添加新的 ClickListener

      Button infoButton = (Button) findViewById(R.id.information);
      infoButton.setOnClickListener(new OnClickListener(){
          public void onClick(View view){
              // here is your button click logic, for example running another activity (page)
              startActivity(new Intent(LayoutPractice.this, SiteMapActivity.class));
          }   
      });
      

      希望对您有所帮助。 PS。代码可能有误,凭记忆写的

      【讨论】:

      • 这很有帮助,但是我应该把它放在哪里
      【解决方案4】:
      Intent intent = new Intent(this, NewActivity.class);
      startActivity(intent);
      

      NewActivity 是您要呈现的活动的名称

      【讨论】:

        猜你喜欢
        • 2013-03-09
        • 1970-01-01
        • 2014-10-18
        • 2016-02-13
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多