使用按钮打开新视图

时间:2011-03-30 05:42:53

标签: android

我想要合并两个应用程序。人们发现管道的尺寸,一个找到管道的尺寸。这两个应用程序都是单独工作的,但我想将它们组合起来,以便当新应用程序最初为用户提供带有两个按钮的简单视图时,他们可以选择是否需要管道或管道。设置这两个按钮视图很简单,但调用与两个独立的其他应用程序关联的其他视图是我遇到的问题。

例如,我的单个应用程序的.java文件是这样的:

package com.examples.pipe;

import com.examples.pipe.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;


public class pipe extends Activity {



    /**
     * Initialize a bunch of stuff
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        /**
         * derived classes that use onCreate() overrides must always call the super constructor
         */
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.Spinner01);

     /**
     * Do a bunch of calculations and post the results in main.xml
     */

管道应用的类似代码。我最初认为制作我的com.examples.pipe和com.examples.tubing应用程序库是由一个简单的按钮应用程序调用将是最好的方法,但无法搞清楚(android网站上的tictactoe示例似乎更多比我想做的更复杂。)

我以为我可以使用这个新应用程序的onCreate调用另一个应用程序button_call

setContentView(R.layout.button_main);

然后使用onClick来调用我已编写的各个应用程序的布局文件夹中的.xml文件。唉,我还没有办法做到这一点。

我有这个应用程序写在Palm Pre(http://www.myappbox.com/search/?q=pipe+and+tube)上,但我正在尝试为Android开发它虽然代码不是完全相似的时候是来调用不同的观点。

4 个答案:

答案 0 :(得分:2)

您可能想要使用多项活动。

请参阅Activities,然后再参考Starting An Activity

简而言之,您可以将活动视为与网页类似。大多数情况下,当用户点击网络浏览器中的按钮时,会触发查询(Android中为Intent),这会将用户带到新页面(Android中为Activity)。带有两个按钮的初始屏幕将是一个活动,管道和管道部分将是两个单独的附加活动。

答案 1 :(得分:2)

您可以按照以下方式执行此操作。 用两个按钮制作一个xml,即管道和管道 在xml的java文件中,为按钮设置监听器。 在听众中,尝试开始一项新活动。

xml文件可以是这样的,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#9999FF"
    >
 <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal"
        android:text="Main Menu" 
        android:textStyle="bold" 
        android:textSize="15px"
        android:paddingTop="10px" 
        android:paddingBottom="10px"
        android:textColor="#FFFFFF"
        android:background="#9966FF" 
        />

            <Button android:layout_width="wrap_content" android:text="Purchase Requisition" android:layout_marginLeft="60px"
                android:layout_height="35px" android:id="@+id/purchaseRequisitonButton" android:layout_marginTop="40px"
            />

            <Button android:layout_width="wrap_content" android:text="Purchase Order" android:layout_marginLeft="65px"
                android:layout_height="35px" android:id="@+id/purchaseOrderButton" android:layout_marginTop="10px"
            />



</LinearLayout>

这样的Java文件可以像......

package guide.training.iApproval;

import android.accounts.OnAccountsUpdateListener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);


        Button purchaseRequisitionButton = (Button) findViewById(R.id.purchaseRequisitonButton);
        Button purchaseOrderButton = (Button) findViewById(R.id.purchaseOrderButton);



        //Listener for the Purchase Requisition button
        purchaseRequisitionButton.setOnClickListener
        (new Button.OnClickListener()
            {
            public void onClick(View v)
                {
                    Intent intent = new Intent(MainMenu.this,guide.training.iApproval.myJava.class);
                    startActivity(intent);

                }
            }
        );

      //Listener for the Purchase Order button
        purchaseOrderButton.setOnClickListener
        (new Button.OnClickListener()
            {
            public void onClick(View v)
                {


                }
            }
        );

    }
}

答案 2 :(得分:1)

这种方法对我有用

View.OnClickListener handler = new View.OnClickListener(){

    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.button1: 
                  setContentView(R.layout.layout1);
                  break;
            case R.id.button2: 

                setContentView(R.layout.layout2);
                break;
            case R.id.button3: 

                 setContentView(R.layout.layout3);
                 break;
            case R.id.button4: 

                setContentView(R.layout.layout4);
                break;

              }
    }

};

findViewById(R.id.button1).setOnClickListener(handler);
findViewById(R.id.button2).setOnClickListener(handler);
findViewById(R.id.button3).setOnClickListener(handler);
findViewById(R.id.button4).setOnClickListener(handler);

}

答案 3 :(得分:0)

如果要将应用程序分开,请使用以下命令激发所需的应用程序:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.pkg.address","com.pkg.address.MainActivity"));
startActivity(intent);

在每个“管道”和“管道”应用中,注册ACTION_MAIN意图以便启动它。阅读Intents and Intent Filters

相关问题