在片段类活动 [英] Activity in fragment class

查看:113
本文介绍了在片段类活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我创建了一个navigationDrawer和我有一个问题

我需要把我的活动

  

ActivitySec

 包com.calculatorvamal.app;

进口android.app.Activity;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.RadioButton;
进口android.widget.TextView;
进口android.widget.Toast;


公共类SecondAct2扩展活动实现TextWatcher {


    私人的EditText结果;
    私人字符串操作;
    私人的EditText editYear;
    私人的EditText editCapacity;
    私人按钮计算;
    私人单选radioD;
    私人单选radioB;


    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_home2);
        TextView的年份=(TextView中)findViewById(R.id.Year);

        TextView的容量=(TextView中)findViewById(R.id.C​​apacity);
        editYear =(EditText上)findViewById(R.id.editYear);
        editYear.addTextChangedListener(本);
        editCapacity =(EditText上)findViewById(R.id.editCapacity);
        最后的EditText结果=(EditText上)findViewById(R.id.Result);
        最终按钮计算值=(按钮)findViewById(R.id.calc);
        最后单选radioD =(单选)findViewById(R.id.radioD);
        最后单选radioB =(单选)findViewById(R.id.radioB);

        TextWatcher监听器=新TextWatcher(){
            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            }

            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
            }

            @覆盖
            公共无效afterTextChanged(编辑S){

                如果(editYear.getText()的toString()长度()== 0 || editCapacity.getText()的toString()长度()== 0){
                    calc.setEnabled(假);
                } 其他 {
                    calc.setEnabled(真正的);
                }

            }
        };

        editYear.addTextChangedListener(听众);
        editCapacity.addTextChangedListener(听众);

        calc.setOnClickListener(新View.OnClickListener(){


                                    @覆盖
                                    公共无效的onClick(视图v){
                                        INT edityear_value =的Integer.parseInt(editYear.getText()的toString()); //转换为intger
                                        双重结果= 0;
                                        INT NumberCapacity =的Integer.parseInt(editCapacity.getText()的toString()); //转换为intger

                                        如果(radioB.isChecked()){


                                            开关(edityear_value){

                                                案例2004年:
                                                    操作=2004;
                                                    打破;
                                                案例2005年:
                                                    操作=2005;
                                                    打破;
                                                案例2006年:
                                                    操作=2006;
                                                    打破;
                                                案例2007年:
                                                    操作=2007;
                                                    打破;
                                                案例2008年:
                                                    操作=2007;
                                                    打破;
                                                案例2009年:
                                                    操作=2009;
                                                    打破;
                                                案例2010:
                                                    操作=2009;
                                                    打破;
                                                案例2011:
                                                    操作=2011;
                                                    打破;
                                                案例2012:
                                                    操作=2011;
                                                    打破;
                                                案例2013:
                                                    操作=2013​​;
                                                    打破;
                                                案例2014年:
                                                    操作=2013​​;
                                                    打破;
                                                默认:

                                                    操作=错误;

                                            }

                                        }否则如果(radioD.isChecked()){

                                            开关(edityear_value){

                                                案例2004年:
                                                    操作=20041;
                                                    打破;
                                                案例2005年:
                                                    操作=20051;
                                                    打破;
                                                案例2006年:
                                                    操作=20061;
                                                    打破;
                                                案例2007年:
                                                    操作=20071;
                                                    打破;
                                                案例2008年:
                                                    操作=20071;
                                                    打破;
                                                案例2009年:
                                                    操作=20091;
                                                    打破;
                                                案例2010:
                                                    操作=20091;
                                                    打破;
                                                案例2011:
                                                    操作=20111;
                                                    打破;
                                                案例2012:
                                                    操作=20111;
                                                    打破;
                                                案例2013:
                                                    操作=20131;
                                                    打破;
                                                案例2014年:
                                                    操作=20131;
                                                    打破;
                                                默认:

                                                    操作=错误;

                                            }
                                        }


                                        // BENZIN


                                        如果(NumberCapacity大于0&安培;&安培; NumberCapacity&其中; = 1000){

                                            如果(Operation.equals(2004)){
                                                结果= NumberCapacity * 0.52;

                                            }
                                            如果(Operation.equals(2005)){
                                                结果= NumberCapacity * 0.50;

                                            }
                                            如果(Operation.equals(2006)){
                                                结果= NumberCapacity * 0.47;

                                            }
                                            如果(Operation.equals(2007)){
                                                结果= NumberCapacity * 0.44;
                                            }
                                            如果(Operation.equals(2009)){
                                                结果= NumberCapacity * 0.43;

                                            }
                                            如果(Operation.equals(2009)){
                                                结果= NumberCapacity * 0.43;

                                            }
                                            如果(Operation.equals(2011)){
                                                结果= NumberCapacity * 0.42;

                                            }

                                            如果(Operation.equals(2013​​)){
                                                结果= NumberCapacity * 0.40;

                                            }
                                        }

                                        如果(NumberCapacity> 1001安培;&安培; NumberCapacity< = 1500){


                                            如果(Operation.equals(2004)){
                                                结果= NumberCapacity * 0.67;

                                            }
                                            如果(Operation.equals(2005)){
                                                结果= NumberCapacity * 0.63;
                                            }
                                            如果(Operation.equals(2006)){
                                                结果= NumberCapacity * 0.60;
                                            }
                                            如果(Operation.equals(2007)){
                                                结果= NumberCapacity * 0.56;
                                            }
                                            如果(Operation.equals(2009)){
                                                结果= NumberCapacity * 0.55;
                                            }
                                            如果(Operation.equals(2011)){
                                                结果= NumberCapacity * 0.54;
                                            }
                                            如果(Operation.equals(2013​​)){
                                                结果= NumberCapacity * 0.52;
                                            }

                                        }
                                        如果(NumberCapacity> = 1501&功放;&安培; NumberCapacity< = 2000){


                                            如果(Operation.equals(2004)){
                                                结果= NumberCapacity * 1.00;
                                            }
                                            如果(Operation.equals(2005)){
                                                结果= NumberCapacity * 0.95;
                                            }
                                            如果(Operation.equals(2006)){
                                                结果= NumberCapacity * 0.89;
                                            }
                                            如果(Operation.equals(2007)){
                                                结果= NumberCapacity * 0.84;
                                            }
                                            如果(Operation.equals(2009)){
                                                结果= NumberCapacity * 0.83;
                                            }
                                            如果(Operation.equals(2011)){
                                                结果= NumberCapacity * 0.82;
                                            }
                                            如果(Operation.equals(2013​​)){
                                                结果= NumberCapacity * 0.80;
                                            }
                                        }
                                        如果(NumberCapacity> = 2001&功放;&安培; NumberCapacity< = 3000){


                                            如果(Operation.equals(2004)){
                                                结果= NumberCapacity * 1.59;
                                            }
                                            如果(Operation.equals(2005)){
                                                结果= NumberCapacity * 1.52;
                                            }
                                            如果(Operation.equals(2006)){
                                                结果= NumberCapacity * 1.44;
                                            }
                                            如果(Operation.equals(2007)){
                                                结果= NumberCapacity * 1.36;
                                            }
                                            如果(Operation.equals(2009)){
                                                结果= NumberCapacity * 1.35;
                                            }
                                            如果(Operation.equals(2011)){
                                                结果= NumberCapacity * 1.34;
                                            }
                                            如果(Operation.equals(2013​​)){
                                                结果= NumberCapacity * 1.32;
                                            }
                                        }
                                        如果(NumberCapacity> = 3000){


                                            如果(Operation.equals(2004)){
                                                结果= NumberCapacity * 3.57;
                                            }
                                            如果(Operation.equals(2005)){
                                                结果= NumberCapacity * 3.56;
                                            }
                                            如果(Operation.equals(2006)){
                                                结果= NumberCapacity * 3.55;
                                            }
                                            如果(Operation.equals(2007)){
                                                结果= NumberCapacity * 3.54;
                                            }
                                            如果(Operation.equals(2009)){
                                                结果= NumberCapacity * 3.53;
                                            }
                                            如果(Operation.equals(2011)){
                                                结果= NumberCapacity * 3.52;
                                            }
                                            如果(Operation.equals(2013​​)){
                                                结果= NumberCapacity * 3.50;
                                            }
                                        }

                                        //柴油机
                                        如果(NumberCapacity< = 1500){

                                            如果(Operation.equals(20041)){
                                                结果= NumberCapacity * 0.67;
                                            }
                                            如果(Operation.equals(20051)){
                                                结果= NumberCapacity * 0.63;
                                            }
                                            如果(Operation.equals(20061)){
                                                结果= NumberCapacity * 0.60;
                                            }
                                            如果(Operation.equals(20071)){
                                                结果= NumberCapacity * 0.56;
                                            }
                                            如果(Operation.equals(20091)){
                                                结果= NumberCapacity * 0.55;
                                            }
                                            如果(Operation.equals(20111)){
                                                结果= NumberCapacity * 0.54;
                                            }
                                            如果(Operation.equals(20131)){
                                                结果= NumberCapacity * 0.52;
                                            }
                                        }
                                        如果(NumberCapacity> = 1501&功放;&安培; NumberCapacity< = 2500){


                                            如果(Operation.equals(20041)){
                                                结果= NumberCapacity * 1.59;
                                            }
                                            如果(Operation.equals(20051)){
                                                结果= NumberCapacity * 1.52;
                                            }
                                            如果(Operation.equals(20061)){
                                                结果= NumberCapacity * 1.44;
                                            }
                                            如果(Operation.equals(20071)){
                                                结果= NumberCapacity * 1.36;
                                            }
                                            如果(Operation.equals(20091)){
                                                结果= NumberCapacity * 1.35;
                                            }
                                            如果(Operation.equals(20111)){
                                                结果= NumberCapacity * 1.34;
                                            }
                                            如果(Operation.equals(20131)){
                                                结果= NumberCapacity * 1.32;
                                            }
                                        }
                                        如果(NumberCapacity> = 2501){


                                            如果(Operation.equals(20041)){
                                                结果= NumberCapacity * 3.57;
                                            }
                                            如果(Operation.equals(20051)){
                                                结果= NumberCapacity * 3.56;
                                            }
                                            如果(Operation.equals(20061)){
                                                结果= NumberCapacity * 3.55;
                                            }
                                            如果(Operation.equals(20071)){
                                                结果= NumberCapacity * 3.54;
                                            }
                                            如果(Operation.equals(20091)){
                                                结果= NumberCapacity * 3.53;
                                            }
                                            如果(Operation.equals(20111)){
                                                结果= NumberCapacity * 3.52;
                                            }
                                            如果(Operation.equals(20131)){
                                                结果= NumberCapacity * 3.50;

                                            }

                                        }
                                        Result.setText(将String.valueOf(结果)); //设置后的价值计算的话

                                        //设置后的价值计算的话


                                    }
                                }
        );


    }


    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){

    }

    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){


    }

    @覆盖
    公共无效afterTextChanged(编辑S){
        尝试 {
            INT无=的Integer.parseInt(s.toString());
            如果(否10 9){
                Toast.makeText(这一点,Autovehicolul NU trebuie SA呸迈vechi日10提肛,Toast.LENGTH_SHORT).show();
            }
        }
        赶上(NumberFormatException异常E){}

    }
}
 

在我的片段CLAS

  

Fragment.class

 包com.calculatorvamal.app;

进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;

公共类HomeFragment扩展片段{

    公共HomeFragment(){}


    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,假);


        返回rootView;
    }


}
 

我尝试做另一种方式与意图,并开始活动,但(在启动时30,40毫秒的延迟),它给了我一个滞后,当活动开始。 下跌免费给任何建议,谢谢你。

解决方案

我想你误会或没有就如何碎片和活动之间进行通信读了起来。你应该阅读如何做到这一点: https://developer.android.com /training/basics/fragments/communicating.html

你也不要混淆你的片段逻辑和你的活动的逻辑。操作与方面或视图(如按钮,点击监听器和列表视图创建等),从你的谨慎片段及其布局进行应包含在片段类。如果你需要从一个结果传递给其他的各种原因,那么你可以像这样单独的实体之间的沟通:

从活动传递的信息片段:

要发送信息,你只需要在片段中定义的方法来接受它,并调用从活动片段:

 公共类MyActivity扩展活动
{
    私人MyFragment myFragment;

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_holder);
        myFragment =新MyFragment();
        getSupportFragmentManager()的BeginTransaction()
            。新增(R.id.fragment_container,myFragment).commit();
    }

    私人无效testFragmentCommunication(字符串消息)
    {
        myFragment.recievedCommunication(消息);
    }
}
 


 公共类MyFragment扩展片段
{
    ...

    公共布尔recievedCommunication(字符串消息)
    {
        Log.d(MyFragment,从活动:收到的沟通:+消息);
    }
    ...
}
 

从片段信息传递到活动:

不过,如果你需要传递的信息从你的片段到你的活动回来,你需要定义一个接口用起来:

 公共类MyFragment扩展片段
{
    MyActivityInterface activityInterface;

    @覆盖
    公共无效onAttach(活动活动)
    {
        super.onAttach(活动);
        尝试
        {
            activityInterface =(MyActivityInterface)的活动;
        }
        赶上(ClassCastException异常E)
        {
            Log.e(TAG,父活动deosn't实行MyActivityInterface');
            抛出新ClassCastException异常(activity.toString()
                    +必须实现MyActivityInterface);
        }
    }

    公共接口MyAcyivityInterface
    {
        公共无效onMessageReceived();
    }

    私人无效testActivityCommunication()
    {
        activityInterface.onMessageReceived(字符串消息);
    }
}
 


 公共类MyActivity扩展活动实现MyFragment.MyActivityInterface
{
    私人MyFragment myFragment;

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_holder);
        myFragment =新MyFragment();
        getSupportFragmentManager()的BeginTransaction()
            。新增(R.id.container,新PlaceholderFragment())提交()。
    }

    公共无效onMessageReceived(字符串消息)
    {
        Log.d(MyActivity,从片段:收到的沟通:+消息);
    }
}
 

So i created a navigationDrawer and i have a problem

I need to put my activity

ActivitySec

package com.calculatorvamal.app;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


public class SecondAct2 extends Activity implements TextWatcher {


    private EditText Result;
    private String Operation;
    private EditText editYear;
    private EditText editCapacity;
    private Button calc;
    private RadioButton radioD;
    private RadioButton radioB;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home2);
        TextView Year = (TextView) findViewById(R.id.Year);

        TextView Capacity = (TextView) findViewById(R.id.Capacity);
        editYear = (EditText) findViewById(R.id.editYear);
        editYear.addTextChangedListener(this);
        editCapacity = (EditText) findViewById(R.id.editCapacity);
        final EditText Result = (EditText) findViewById(R.id.Result);
        final Button calc = (Button) findViewById(R.id.calc);
        final RadioButton radioD = (RadioButton) findViewById(R.id.radioD);
        final RadioButton radioB = (RadioButton) findViewById(R.id.radioB);

        TextWatcher listener = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                if (editYear.getText().toString().length() == 0 || editCapacity.getText().toString().length() == 0) {
                    calc.setEnabled(false);
                } else {
                    calc.setEnabled(true);
                }

            }
        };

        editYear.addTextChangedListener(listener);
        editCapacity.addTextChangedListener(listener);

        calc.setOnClickListener(new View.OnClickListener() {


                                    @Override
                                    public void onClick(View v) {
                                        int edityear_value = Integer.parseInt(editYear.getText().toString());//Convert to intger
                                        double result = 0;
                                        int NumberCapacity = Integer.parseInt(editCapacity.getText().toString());//Convert to intger

                                        if (radioB.isChecked()) {


                                            switch (edityear_value) {

                                                case 2004:
                                                    Operation = "2004";
                                                    break;
                                                case 2005:
                                                    Operation = "2005";
                                                    break;
                                                case 2006:
                                                    Operation = "2006";
                                                    break;
                                                case 2007:
                                                    Operation = "2007";
                                                    break;
                                                case 2008:
                                                    Operation = "2007";
                                                    break;
                                                case 2009:
                                                    Operation = "2009";
                                                    break;
                                                case 2010:
                                                    Operation = "2009";
                                                    break;
                                                case 2011:
                                                    Operation = "2011";
                                                    break;
                                                case 2012:
                                                    Operation = "2011";
                                                    break;
                                                case 2013:
                                                    Operation = "2013";
                                                    break;
                                                case 2014:
                                                    Operation = "2013";
                                                    break;
                                                default:

                                                    Operation = "error";

                                            }

                                        } else if (radioD.isChecked()) {

                                            switch (edityear_value) {

                                                case 2004:
                                                    Operation = "20041";
                                                    break;
                                                case 2005:
                                                    Operation = "20051";
                                                    break;
                                                case 2006:
                                                    Operation = "20061";
                                                    break;
                                                case 2007:
                                                    Operation = "20071";
                                                    break;
                                                case 2008:
                                                    Operation = "20071";
                                                    break;
                                                case 2009:
                                                    Operation = "20091";
                                                    break;
                                                case 2010:
                                                    Operation = "20091";
                                                    break;
                                                case 2011:
                                                    Operation = "20111";
                                                    break;
                                                case 2012:
                                                    Operation = "20111";
                                                    break;
                                                case 2013:
                                                    Operation = "20131";
                                                    break;
                                                case 2014:
                                                    Operation = "20131";
                                                    break;
                                                default:

                                                    Operation = "error";

                                            }
                                        }


                                        //Benzin


                                        if (NumberCapacity > 0 && NumberCapacity <= 1000) {

                                            if (Operation.equals("2004")) {
                                                result = NumberCapacity * 0.52;

                                            }
                                            if (Operation.equals("2005")) {
                                                result = NumberCapacity * 0.50;

                                            }
                                            if (Operation.equals("2006")) {
                                                result = NumberCapacity * 0.47;

                                            }
                                            if (Operation.equals("2007")) {
                                                result = NumberCapacity * 0.44;
                                            }
                                            if (Operation.equals("2009")) {
                                                result = NumberCapacity * 0.43;

                                            }
                                            if (Operation.equals("2009")) {
                                                result = NumberCapacity * 0.43;

                                            }
                                            if (Operation.equals("2011")) {
                                                result = NumberCapacity * 0.42;

                                            }

                                            if (Operation.equals("2013")) {
                                                result = NumberCapacity * 0.40;

                                            }
                                        }

                                        if (NumberCapacity > 1001 && NumberCapacity <= 1500) {


                                            if (Operation.equals("2004")) {
                                                result = NumberCapacity * 0.67;

                                            }
                                            if (Operation.equals("2005")) {
                                                result = NumberCapacity * 0.63;
                                            }
                                            if (Operation.equals("2006")) {
                                                result = NumberCapacity * 0.60;
                                            }
                                            if (Operation.equals("2007")) {
                                                result = NumberCapacity * 0.56;
                                            }
                                            if (Operation.equals("2009")) {
                                                result = NumberCapacity * 0.55;
                                            }
                                            if (Operation.equals("2011")) {
                                                result = NumberCapacity * 0.54;
                                            }
                                            if (Operation.equals("2013")) {
                                                result = NumberCapacity * 0.52;
                                            }

                                        }
                                        if (NumberCapacity >= 1501 && NumberCapacity <= 2000) {


                                            if (Operation.equals("2004")) {
                                                result = NumberCapacity * 1.00;
                                            }
                                            if (Operation.equals("2005")) {
                                                result = NumberCapacity * 0.95;
                                            }
                                            if (Operation.equals("2006")) {
                                                result = NumberCapacity * 0.89;
                                            }
                                            if (Operation.equals("2007")) {
                                                result = NumberCapacity * 0.84;
                                            }
                                            if (Operation.equals("2009")) {
                                                result = NumberCapacity * 0.83;
                                            }
                                            if (Operation.equals("2011")) {
                                                result = NumberCapacity * 0.82;
                                            }
                                            if (Operation.equals("2013")) {
                                                result = NumberCapacity * 0.80;
                                            }
                                        }
                                        if (NumberCapacity >= 2001 && NumberCapacity <= 3000) {


                                            if (Operation.equals("2004")) {
                                                result = NumberCapacity * 1.59;
                                            }
                                            if (Operation.equals("2005")) {
                                                result = NumberCapacity * 1.52;
                                            }
                                            if (Operation.equals("2006")) {
                                                result = NumberCapacity * 1.44;
                                            }
                                            if (Operation.equals("2007")) {
                                                result = NumberCapacity * 1.36;
                                            }
                                            if (Operation.equals("2009")) {
                                                result = NumberCapacity * 1.35;
                                            }
                                            if (Operation.equals("2011")) {
                                                result = NumberCapacity * 1.34;
                                            }
                                            if (Operation.equals("2013")) {
                                                result = NumberCapacity * 1.32;
                                            }
                                        }
                                        if (NumberCapacity >= 3000) {


                                            if (Operation.equals("2004")) {
                                                result = NumberCapacity * 3.57;
                                            }
                                            if (Operation.equals("2005")) {
                                                result = NumberCapacity * 3.56;
                                            }
                                            if (Operation.equals("2006")) {
                                                result = NumberCapacity * 3.55;
                                            }
                                            if (Operation.equals("2007")) {
                                                result = NumberCapacity * 3.54;
                                            }
                                            if (Operation.equals("2009")) {
                                                result = NumberCapacity * 3.53;
                                            }
                                            if (Operation.equals("2011")) {
                                                result = NumberCapacity * 3.52;
                                            }
                                            if (Operation.equals("2013")) {
                                                result = NumberCapacity * 3.50;
                                            }
                                        }

                                        //Diesel
                                        if (NumberCapacity <= 1500) {

                                            if (Operation.equals("20041")) {
                                                result = NumberCapacity * 0.67;
                                            }
                                            if (Operation.equals("20051")) {
                                                result = NumberCapacity * 0.63;
                                            }
                                            if (Operation.equals("20061")) {
                                                result = NumberCapacity * 0.60;
                                            }
                                            if (Operation.equals("20071")) {
                                                result = NumberCapacity * 0.56;
                                            }
                                            if (Operation.equals("20091")) {
                                                result = NumberCapacity * 0.55;
                                            }
                                            if (Operation.equals("20111")) {
                                                result = NumberCapacity * 0.54;
                                            }
                                            if (Operation.equals("20131")) {
                                                result = NumberCapacity * 0.52;
                                            }
                                        }
                                        if (NumberCapacity >= 1501 && NumberCapacity <= 2500) {


                                            if (Operation.equals("20041")) {
                                                result = NumberCapacity * 1.59;
                                            }
                                            if (Operation.equals("20051")) {
                                                result = NumberCapacity * 1.52;
                                            }
                                            if (Operation.equals("20061")) {
                                                result = NumberCapacity * 1.44;
                                            }
                                            if (Operation.equals("20071")) {
                                                result = NumberCapacity * 1.36;
                                            }
                                            if (Operation.equals("20091")) {
                                                result = NumberCapacity * 1.35;
                                            }
                                            if (Operation.equals("20111")) {
                                                result = NumberCapacity * 1.34;
                                            }
                                            if (Operation.equals("20131")) {
                                                result = NumberCapacity * 1.32;
                                            }
                                        }
                                        if (NumberCapacity >= 2501) {


                                            if (Operation.equals("20041")) {
                                                result = NumberCapacity * 3.57;
                                            }
                                            if (Operation.equals("20051")) {
                                                result = NumberCapacity * 3.56;
                                            }
                                            if (Operation.equals("20061")) {
                                                result = NumberCapacity * 3.55;
                                            }
                                            if (Operation.equals("20071")) {
                                                result = NumberCapacity * 3.54;
                                            }
                                            if (Operation.equals("20091")) {
                                                result = NumberCapacity * 3.53;
                                            }
                                            if (Operation.equals("20111")) {
                                                result = NumberCapacity * 3.52;
                                            }
                                            if (Operation.equals("20131")) {
                                                result = NumberCapacity * 3.50;

                                            }

                                        }
                                        Result.setText(String.valueOf(result));//Set the value after calculate it

                                        //Set the value after calculate it


                                    }
                                }
        );


    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {


    }

    @Override
    public void afterTextChanged(Editable s) {
        try {
            int no = Integer.parseInt(s.toString());
            if (no < 9) {
                Toast.makeText(this, "Autovehicolul nu trebuie sa fie mai vechi de 10 ani", Toast.LENGTH_SHORT).show();
            }
        }
        catch(NumberFormatException e){}

    }
}

in my fragment clas

Fragment.class

package com.calculatorvamal.app;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    public HomeFragment(){}


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

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);


        return rootView;
    }


}

i tried doing it another way with intent and start activity but it gives me a lag(30,40 milliseconds delay when starting) when activity is started. Fell free to give any suggestion,thank you.

解决方案

I think that you have misunderstood or not read up on how to communicate between Fragments and Activities. You should read up on how to do so: https://developer.android.com/training/basics/fragments/communicating.html.

Also you should not mix your Fragment logic and your Activity logic. Actions to be performed with aspects or Views (such as button click listeners and list view creation etc) from your discreet Fragments and their layouts should be contained in the Fragment class. If you need to pass results from one to the other for various reasons then you can communicate between the separate entities like so:

Passing information from Activity to Fragment:

To send information to a fragment you need only define a method in the Fragment to receive it and call that from your Activity:

public class MyActivity extends Activity
{
    private MyFragment myFragment;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_holder);
        myFragment = new MyFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, myFragment).commit();
    }

    private void testFragmentCommunication(String message)
    {
        myFragment.recievedCommunication(message);
    }
}


public class MyFragment extends Fragment
{
    ...

    public boolean recievedCommunication(String message)
    {
        Log.d("MyFragment", "Recieved communication from Activity: " + message);
    }
    ...
}

Passing information from Fragment to Activity:

However if you need to pass information back from your Fragment to your Activity you will need to define an Interface for them to use:

public class MyFragment extends Fragment
{
    MyActivityInterface activityInterface;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            activityInterface = (MyActivityInterface) activity;
        }
        catch (ClassCastException e)
        {
            Log.e(TAG, "Parent Activity deosn't implement 'MyActivityInterface'");
            throw new ClassCastException(activity.toString()
                    + " must implement MyActivityInterface");
        }
    }

    public Interface MyAcyivityInterface
    {
        public void onMessageReceived();
    }

    private void testActivityCommunication()
    {
        activityInterface.onMessageReceived(String message);
    }
}


public class MyActivity extends Activity implements MyFragment.MyActivityInterface
{
    private MyFragment myFragment;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_holder);
        myFragment = new MyFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
    }

    public void onMessageReceived(String message)
    {
        Log.d("MyActivity", "Recieved communication from Fragment: " + message);
    }
}

这篇关于在片段类活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆