Android 如何在SherlockFragment中包含活动

Android 如何在SherlockFragment中包含活动,android,Android,嘿,伙计们,我有一个正在开发的应用程序,我有一个计算器,我可以在fragmenttab2.xml中包含布局,但我不知道如何或在哪里调用名为CMainActivity.java的计算器活动 package com.d4a.stzh; import java.text.DecimalFormat; import com.actionbarsherlock.app.SherlockFragmentActivity; import android.annotation.SuppressLint;

嘿,伙计们,我有一个正在开发的应用程序,我有一个计算器,我可以在fragmenttab2.xml中包含布局,但我不知道如何或在哪里调用名为CMainActivity.java的计算器活动

package com.d4a.stzh;

import java.text.DecimalFormat;

import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CMainActivity extends SherlockFragmentActivity implements OnClickListener {

    private TextView calculatorDisplay;
    private static final String DIGITS = "0123456789.";
    private Boolean userIsInTheMiddleOfTypingANumber = false;

    DecimalFormat df = new DecimalFormat("@###########");

    CalculatorBrain brain;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hide the status bar and other OS-level chrome
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cmain);

        brain = new CalculatorBrain();

        calculatorDisplay = (TextView) findViewById(R.id.textView1);

        df.setMinimumFractionDigits(0);
        df.setMinimumIntegerDigits(1);
        df.setMaximumIntegerDigits(8);

        findViewById(R.id.button0).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
        findViewById(R.id.button7).setOnClickListener(this);
        findViewById(R.id.button8).setOnClickListener(this);
        findViewById(R.id.button9).setOnClickListener(this);

        findViewById(R.id.buttonAdd).setOnClickListener(this);
        findViewById(R.id.buttonSubtract).setOnClickListener(this);
        findViewById(R.id.buttonMultiply).setOnClickListener(this);
        findViewById(R.id.buttonDivide).setOnClickListener(this);
        findViewById(R.id.buttonToggleSign).setOnClickListener(this);
        findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
        findViewById(R.id.buttonEquals).setOnClickListener(this);
        findViewById(R.id.buttonClear).setOnClickListener(this);
        findViewById(R.id.buttonClearMemory).setOnClickListener(this);
        findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
        findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);
        findViewById(R.id.buttonRecallMemory).setOnClickListener(this);

        // The following buttons only exist in layout-land (Landscape mode) and require extra attention.
        // The messier option is to place the buttons in the regular layout too and set android:visibility="invisible". 
        if (findViewById(R.id.buttonSquareRoot) != null) {
            findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
        }   
        if (findViewById(R.id.buttonInvert) != null) {
            findViewById(R.id.buttonInvert).setOnClickListener(this);
        }
        if (findViewById(R.id.buttonCos) != null) {
            findViewById(R.id.buttonCos).setOnClickListener(this);
        }   
        if (findViewById(R.id.buttonSin) != null) {
            findViewById(R.id.buttonSin).setOnClickListener(this);
        }

//      Another way to hide the window title and actionbar, but only in newer sdk's
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            ActionBar actionBar = getActionBar();
//            actionBar.setDisplayShowHomeEnabled(false);
//            actionBar.setDisplayShowTitleEnabled(false);
//            actionBar.hide();
//        }
    }

    // @Override
    public void onClick(View view) {

        String buttonPressed = ((Button) view).getText().toString();
        // String digits = "0123456789.";

        if (DIGITS.contains(buttonPressed)) {
            // digit was pressed
            if (userIsInTheMiddleOfTypingANumber) {
                calculatorDisplay.append(buttonPressed);
            } else {
                calculatorDisplay.setText(buttonPressed);
                userIsInTheMiddleOfTypingANumber = true;
            }
        } else {
            // operation was pressed
            if (userIsInTheMiddleOfTypingANumber) {
                brain.setOperand(Double.parseDouble(calculatorDisplay.getText().toString()));
                userIsInTheMiddleOfTypingANumber = false;
            }

            brain.performOperation(buttonPressed);
            calculatorDisplay.setText(df.format(brain.getResult()));

        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save variables on screen orientation change
        outState.putDouble("OPERAND", brain.getResult());
        outState.putDouble("MEMORY", brain.getMemory());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore variables on screen orientation change
        brain.setOperand(savedInstanceState.getDouble("OPERAND"));
        brain.setMemory(savedInstanceState.getDouble("MEMORY"));
        calculatorDisplay.setText(df.format(brain.getResult()));
    }
CMainActivity.java

package com.d4a.stzh;

import java.text.DecimalFormat;

import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CMainActivity extends SherlockFragmentActivity implements OnClickListener {

    private TextView calculatorDisplay;
    private static final String DIGITS = "0123456789.";
    private Boolean userIsInTheMiddleOfTypingANumber = false;

    DecimalFormat df = new DecimalFormat("@###########");

    CalculatorBrain brain;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hide the status bar and other OS-level chrome
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cmain);

        brain = new CalculatorBrain();

        calculatorDisplay = (TextView) findViewById(R.id.textView1);

        df.setMinimumFractionDigits(0);
        df.setMinimumIntegerDigits(1);
        df.setMaximumIntegerDigits(8);

        findViewById(R.id.button0).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
        findViewById(R.id.button7).setOnClickListener(this);
        findViewById(R.id.button8).setOnClickListener(this);
        findViewById(R.id.button9).setOnClickListener(this);

        findViewById(R.id.buttonAdd).setOnClickListener(this);
        findViewById(R.id.buttonSubtract).setOnClickListener(this);
        findViewById(R.id.buttonMultiply).setOnClickListener(this);
        findViewById(R.id.buttonDivide).setOnClickListener(this);
        findViewById(R.id.buttonToggleSign).setOnClickListener(this);
        findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
        findViewById(R.id.buttonEquals).setOnClickListener(this);
        findViewById(R.id.buttonClear).setOnClickListener(this);
        findViewById(R.id.buttonClearMemory).setOnClickListener(this);
        findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
        findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);
        findViewById(R.id.buttonRecallMemory).setOnClickListener(this);

        // The following buttons only exist in layout-land (Landscape mode) and require extra attention.
        // The messier option is to place the buttons in the regular layout too and set android:visibility="invisible". 
        if (findViewById(R.id.buttonSquareRoot) != null) {
            findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
        }   
        if (findViewById(R.id.buttonInvert) != null) {
            findViewById(R.id.buttonInvert).setOnClickListener(this);
        }
        if (findViewById(R.id.buttonCos) != null) {
            findViewById(R.id.buttonCos).setOnClickListener(this);
        }   
        if (findViewById(R.id.buttonSin) != null) {
            findViewById(R.id.buttonSin).setOnClickListener(this);
        }

//      Another way to hide the window title and actionbar, but only in newer sdk's
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            ActionBar actionBar = getActionBar();
//            actionBar.setDisplayShowHomeEnabled(false);
//            actionBar.setDisplayShowTitleEnabled(false);
//            actionBar.hide();
//        }
    }

    // @Override
    public void onClick(View view) {

        String buttonPressed = ((Button) view).getText().toString();
        // String digits = "0123456789.";

        if (DIGITS.contains(buttonPressed)) {
            // digit was pressed
            if (userIsInTheMiddleOfTypingANumber) {
                calculatorDisplay.append(buttonPressed);
            } else {
                calculatorDisplay.setText(buttonPressed);
                userIsInTheMiddleOfTypingANumber = true;
            }
        } else {
            // operation was pressed
            if (userIsInTheMiddleOfTypingANumber) {
                brain.setOperand(Double.parseDouble(calculatorDisplay.getText().toString()));
                userIsInTheMiddleOfTypingANumber = false;
            }

            brain.performOperation(buttonPressed);
            calculatorDisplay.setText(df.format(brain.getResult()));

        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save variables on screen orientation change
        outState.putDouble("OPERAND", brain.getResult());
        outState.putDouble("MEMORY", brain.getMemory());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore variables on screen orientation change
        brain.setOperand(savedInstanceState.getDouble("OPERAND"));
        brain.setMemory(savedInstanceState.getDouble("MEMORY"));
        calculatorDisplay.setText(df.format(brain.getResult()));
    }
}

FragmantTab2.java:

package com.d4a.stzh;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import com.d4a.stzh.CMainActivity;
import com.d4a.stzh.CalculatorBrain;

import com.actionbarsherlock.app.SherlockFragment;

public class FragmentTab2 extends SherlockFragment {
    private Button appbtn;
    private Button webbtn;
    private Button toolsbttn;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Get the view from fragmenttab2.xml
        View view = inflater.inflate(R.layout.fragmenttab2, container, false);



        //Get the button from layout
        appbtn = (Button) view.findViewById(R.id.app);
        webbtn = (Button) view.findViewById(R.id.web);
        toolsbttn = (Button) view.findViewById(R.id.tools);



        appbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FragmentTab2.this.getActivity(), MyLauncherActivity.class);
                startActivity(intent);

            }


            });

        webbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String url = "http://www.google.com";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });

        //tools button 
        toolsbttn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName("com.d4a.stz","com.d4a.stz.MainActivity"));
                intent.putExtra("grace", "Hi");
                startActivity(intent);

            }


            });

        return view;
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }

}
我是android新手,所以请不要妄下判断

提前谢谢

Rapsong11

Android使用它来启动活动

启动
CMA非活动的代码如下所示:

Intent intent = new Intent(this, CMainActivity.class);
startActivity(intent);

我将把Intent=newintent放在哪里(这是CMainActivity.class);星触觉(意向);无论您想在哪里启动活动。如果您有“使用计算器”按钮,然后它可能会进入该按钮的onClickListener。它不会通过按钮单击启动计算器是FragmentTab2的一部分,所以有其他建议吗?它不会通过按钮单击启动计算器是FragmentTab2的一部分,所以有其他建议吗?片段不能包含活动。不过,活动可以包含一个或多个片段。您可以将活动视为计算机上的窗口,将片段视为该窗口的内容。