如何将EditText更改为int并将其显示在新页面中

时间:2014-11-07 07:04:40

标签: android eclipse string int

我现在正面临这个问题。请帮帮我......

我有这个editText文本框。下面是代码。

      <EditText
    android:id="@+id/nog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number"/>

我希望用户只输入数字,所以我已经声明了inputType。 用户在EditText中输入数字后,他们将点击一个继续按钮,该按钮将显示他们刚才输入的数字(这意味着它将显示在新页面中)。

问题是,我无法从EditText文本框中检索值。

这是显示他们之前输入的数字的页面代码。

   <TextView
    android:id="@+id/textView27"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView25"
    android:text="Number of guest(s) : " />

问题是我不知道如何在EditText框中获取值。

 intent.putExtra("", .isChecked());

代码意图无法正常理解....请帮帮我

最新消息

doReceipt.java

     package com.example.mobilecatering;

  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.TextView;

  public class doReceipt extends Activity

 {
boolean nsPutih, nsBeriyani,nsMinyak,aymKurma,aymLemak,aymKari,aymRendang,aymMerah,
prtAsam, dggTomato, dggSinggang, dggMerah,kerabuTaugeh, pindangKacang,jelatah,dalcaSayur,papadom,
kariIkan,ikanMasin, sirapAis, oren,tehTarik,agarAgar, buburKacang,kuih, price;



TextView tvOutput1,tvOutput2, tvOutput3, tvOutput4, tvOutput5, tvOutput6, tvOutput7, tvOutput8, tvOutput9, tvOutput10, 
tvOutput11, tvOutput12, tvOutput13, tvOutput14, tvOutput15, tvOutput16, tvOutput17, tvOutput18, tvOutput19, tvOutput20,
tvOutput21, tvOutput22, tvOutput23, tvOutput24, tvOutput25, tvOutput26, tvOutput27, tvOutput28;

public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.receipt);

        if(getIntent().getIntExtra("nog",0)!=0)
        tvOutput27.setText("Number of guest(s) : "+ getIntent().getIntExtra("nog",0));



      tvOutput1 = (TextView) findViewById(R.id.textView1);
      tvOutput2 = (TextView) findViewById(R.id.textView2);
      tvOutput3 = (TextView) findViewById(R.id.textView3);
      tvOutput4 = (TextView) findViewById(R.id.textView4);
      tvOutput5 = (TextView) findViewById(R.id.textView5);
      tvOutput6 = (TextView) findViewById(R.id.textView6);
      tvOutput7 = (TextView) findViewById(R.id.textView7);
      tvOutput8 = (TextView) findViewById(R.id.textView8);
      tvOutput9 = (TextView) findViewById(R.id.textView9);
      tvOutput10 = (TextView) findViewById(R.id.textView10);
      tvOutput11 = (TextView) findViewById(R.id.textView11);
      tvOutput12 = (TextView) findViewById(R.id.textView12);
      tvOutput13 = (TextView) findViewById(R.id.textView13);
      tvOutput14 = (TextView) findViewById(R.id.textView14);
      tvOutput15 = (TextView) findViewById(R.id.textView15);
      tvOutput16 = (TextView) findViewById(R.id.textView16);
      tvOutput17 = (TextView) findViewById(R.id.textView17);
      tvOutput18 = (TextView) findViewById(R.id.textView18);
      tvOutput19 = (TextView) findViewById(R.id.textView19);
      tvOutput20 = (TextView) findViewById(R.id.textView20);
      tvOutput21 = (TextView) findViewById(R.id.textView21);
      tvOutput22 = (TextView) findViewById(R.id.textView22);
      tvOutput23 = (TextView) findViewById(R.id.textView23);
      tvOutput24 = (TextView) findViewById(R.id.textView24);
      tvOutput25 = (TextView) findViewById(R.id.textView25);
      tvOutput26 = (TextView) findViewById(R.id.textView26);
      tvOutput27 = (TextView) findViewById(R.id.textView27);


      Bundle data = this.getIntent().getExtras();

      nsPutih=data.getBoolean("nsPutih");
      nsBeriyani=data.getBoolean("nsBeriyani");
      nsMinyak=data.getBoolean("nsMinyak");
      aymKurma=data.getBoolean("aymKurma");
      aymLemak=data.getBoolean("aymLemak");
      aymKari=data.getBoolean("aymKari");
      aymRendang=data.getBoolean("aymRendang");
      aymMerah=data.getBoolean("aymMerah");
      prtAsam=data.getBoolean("prtAsam");
      dggTomato=data.getBoolean("dggTomato");
      dggSinggang=data.getBoolean("dggSinggang");
      dggMerah=data.getBoolean("dggMerah");
      kerabuTaugeh=data.getBoolean("kerabuTaugeh");
      pindangKacang=data.getBoolean("pindangKacang");
      jelatah=data.getBoolean("jelatah");
      dalcaSayur=data.getBoolean("dalcaSayur");
      papadom=data.getBoolean("papadom");
      kariIkan=data.getBoolean("kariIkan");
      ikanMasin=data.getBoolean("ikanMasin");
      sirapAis=data.getBoolean("sirapAis");
      oren=data.getBoolean("oren");
      tehTarik=data.getBoolean("tehTarik");
      agarAgar=data.getBoolean("agarAgar");
      buburKacang=data.getBoolean("buburKacang");
      kuih=data.getBoolean("kuih");
      price=data.getBoolean("price");


      double price = 0;


      if(nsPutih==true)
      {

          tvOutput1.setText("Nasi Putih - RM 1.00");
          tvOutput1.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput1.setVisibility(View.GONE);
      }

      if (nsBeriyani==true)
      {
          tvOutput2.setText("Nasi Beriyani - RM 2.00");
          tvOutput2.setVisibility(View.VISIBLE);
          price+=2.0;
      }
      else
      {
          tvOutput2.setVisibility(View.GONE);
      }
      if (nsMinyak==true)
      {
          tvOutput3.setText("Nasi Minyak - RM 1.50");
          tvOutput3.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput3.setVisibility(View.GONE);
      }
      if(aymKurma==true)
      {
          tvOutput4.setText("Ayam Masak Kurma - RM 2.00");
          tvOutput4.setVisibility(View.VISIBLE);
          price+=2;
      }
      else
      {
          tvOutput4.setVisibility(View.GONE);
      }
      if (aymLemak==true)
      {
          tvOutput5.setText("Ayam Masak Lemak - RM 4.00");
          tvOutput5.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput5.setVisibility(View.GONE);
      }

      if (aymKari==true)
      {
          tvOutput6.setText("Ayam Masak Kari - RM 4.00");
          tvOutput6.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput6.setVisibility(View.GONE);
      }

      if (aymRendang==true)
      {
          tvOutput7.setText("Ayam Masak Rendang - RM 4.00");
          tvOutput7.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput7.setVisibility(View.GONE);
      }

      if (aymMerah==true)
      {
          tvOutput8.setText("Ayam Masak Merah - RM 4.00");
          tvOutput8.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput8.setVisibility(View.GONE);
      }

      if (prtAsam==true)
      {
          tvOutput9.setText("Perut Air Asam - RM 3.00");
          tvOutput9.setVisibility(View.VISIBLE);
          price+=3;
      }
      else
      {
          tvOutput9.setVisibility(View.GONE);
      }

      if (dggTomato==true)
      {
          tvOutput10.setText("Daging Masak Tomato - RM 4.00");
          tvOutput10.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput10.setVisibility(View.GONE);
      }

      if (dggSinggang==true)
      {
          tvOutput11.setText("Daging Masak Singgang - RM 4.00");
          tvOutput11.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput11.setVisibility(View.GONE);
      }

      if (dggMerah==true)
      {
          tvOutput12.setText("Daging Masak Merah - RM 4.00");
          tvOutput12.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput12.setVisibility(View.GONE);
      }

      if (kerabuTaugeh==true)
      {
          tvOutput13.setText("Kerabu Taugeh - RM 1.50");
          tvOutput13.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput13.setVisibility(View.GONE);
      }

      if (pindangKacang==true)
      {
          tvOutput14.setText("Pindang Kacang - RM 1.00");
          tvOutput14.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput14.setVisibility(View.GONE);
      }

      if (jelatah==true)
      {
          tvOutput15.setText("Jelatah - RM 2.00");
          tvOutput15.setVisibility(View.VISIBLE);
          price+=2;
      }
      else
      {
          tvOutput15.setVisibility(View.GONE);
      }

      if (dalcaSayur==true)
      {
          tvOutput16.setText("Dalca Sayur - RM 1.50");
          tvOutput16.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput16.setVisibility(View.GONE);
      }

      if (papadom==true)
      {
          tvOutput17.setText("Papadom - RM 0.50");
          tvOutput17.setVisibility(View.VISIBLE);
          price+=0.5;
      }
      else
      {
          tvOutput17.setVisibility(View.GONE);
      }

      if (kariIkan==true)
      {
          tvOutput18.setText("Kari Ikan - RM 1.50");
          tvOutput18.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput18.setVisibility(View.GONE);
      }

      if (ikanMasin==true)
      {
          tvOutput19.setText("Kari Ikan Masin - RM 4.00");
          tvOutput19.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput19.setVisibility(View.GONE);
      }

      if (sirapAis==true)
      {
          tvOutput20.setText("Sirap Ais - RM 1.00");
          tvOutput20.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput20.setVisibility(View.GONE);
      }

      if (oren==true)
      {
          tvOutput21.setText("Oren - RM 1.00");
          tvOutput21.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput21.setVisibility(View.GONE);
      }

      if (tehTarik==true)
      {
          tvOutput22.setText("Teh Tarik - RM 1.20");
          tvOutput22.setVisibility(View.VISIBLE);
          price+=1.2;
      }
      else
      {
          tvOutput22.setVisibility(View.GONE);
      }

      if (agarAgar==true)
      {
          tvOutput23.setText("Agar Agar - RM 1.00");
          tvOutput23.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput23.setVisibility(View.GONE);
      }

      if (buburKacang==true)
      {
          tvOutput24.setText("Bubur Kacang - RM 2.50");
          tvOutput24.setVisibility(View.VISIBLE);
          price+=2.5;
      }
      else
      {
          tvOutput24.setVisibility(View.GONE);
      }

      if (kuih==true)
      {
          tvOutput25.setText("Kuih Muih - RM 1.00");
          tvOutput25.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput25.setVisibility(View.GONE);
      }

      Log.e("MENU PAKEJ A","Total Price: RM "+ price);
         String stringdouble= Double.toString(price);
         tvOutput26.setText("\n\n Total Price : RM "+ stringdouble);

            }
public void goHome(View v)
{
    {
        Intent intent = new Intent(v.getContext(), MainActivity.class);
        startActivityForResult(intent,0);
 }
    }

}

这是mcpakej1.java

package com.example.mobilecatering;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox; 
import android.widget.EditText;

public class mcpakej1 extends Activity 
{
 EditText editText;
CheckBox nsPutih,nsBeriyani,nsMinyak,aymKurma,aymLemak,aymKari,aymRendang,aymMerah,
prtAsam, dggTomato, dggSinggang, dggMerah,kerabuTaugeh, pindangKacang,jelatah,dalcaSayur,papadom,
kariIkan,ikanMasin, sirapAis, oren,tehTarik,agarAgar, buburKacang,kuih;

public void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mcpakej1);


  nsPutih       = (CheckBox) findViewById(R.id.pakejA1);
  nsBeriyani    = (CheckBox) findViewById(R.id.pakejA2);
  nsMinyak      = (CheckBox) findViewById(R.id.pakejA3);
  aymKurma      = (CheckBox) findViewById(R.id.pakejA4);
  aymLemak      = (CheckBox) findViewById(R.id.pakejA5);
  aymKari       = (CheckBox) findViewById(R.id.pakejA6);
  aymRendang    = (CheckBox) findViewById(R.id.pakejA7);
  aymMerah      = (CheckBox) findViewById(R.id.pakejA8);
  prtAsam       = (CheckBox) findViewById(R.id.pakejA9);
  dggTomato     = (CheckBox) findViewById(R.id.pakejA10);
  dggSinggang   = (CheckBox) findViewById(R.id.pakejA11);
  dggMerah      = (CheckBox) findViewById(R.id.pakejA12);
  kerabuTaugeh  = (CheckBox) findViewById(R.id.pakejA13);
  pindangKacang = (CheckBox) findViewById(R.id.pakejA14);
  jelatah       = (CheckBox) findViewById(R.id.pakejA15);
  dalcaSayur    = (CheckBox) findViewById(R.id.pakejA16);
  papadom       = (CheckBox) findViewById(R.id.pakejA17);
  kariIkan      = (CheckBox) findViewById(R.id.pakejA18);
  ikanMasin     = (CheckBox) findViewById(R.id.pakejA19);
  sirapAis      = (CheckBox) findViewById(R.id.pakejA20);
  oren          = (CheckBox) findViewById(R.id.pakejA21);
  tehTarik      = (CheckBox) findViewById(R.id.pakejA22);
  agarAgar      = (CheckBox) findViewById(R.id.pakejA23);
  buburKacang   = (CheckBox) findViewById(R.id.pakejA24);
  kuih          = (CheckBox) findViewById(R.id.pakejA25);


}

public void goHome(View v){
{
    Intent intent = new Intent(v.getContext(), MainActivity.class);
    startActivityForResult(intent,0);}
}

public void goReceipt(View v) 
{
    Intent intent = new Intent(v.getContext(), doReceipt.class);

    intent.putExtra("nsBeriyani", nsBeriyani.isChecked());
    intent.putExtra("nsPutih", nsPutih.isChecked());
    intent.putExtra("nsMinyak", nsMinyak.isChecked());
    intent.putExtra("aymKurma", aymKurma.isChecked());
    intent.putExtra("aymKari", aymKari.isChecked());
    intent.putExtra("aymRendang", aymRendang.isChecked());
    intent.putExtra("aymMerah", aymMerah.isChecked());
    intent.putExtra("prtAsam", prtAsam.isChecked());
    intent.putExtra("dggTomato", dggTomato.isChecked());
    intent.putExtra("dggSinggang", dggSinggang.isChecked());
    intent.putExtra("dggMerah", dggMerah.isChecked());
    intent.putExtra("kerabuTaugeh", kerabuTaugeh.isChecked());
    intent.putExtra("pindangKacang", pindangKacang.isChecked());
    intent.putExtra("jelatah", jelatah.isChecked());
    intent.putExtra("dalcaSayur", dalcaSayur.isChecked());
    intent.putExtra("papadom", papadom.isChecked());
    intent.putExtra("kariIkan", kariIkan.isChecked());
    intent.putExtra("ikanMasin", ikanMasin.isChecked());
    intent.putExtra("sirapAis", sirapAis.isChecked());
    intent.putExtra("oren", oren.isChecked());
    intent.putExtra("tehTarik", tehTarik.isChecked());
    intent.putExtra("agarAgar", agarAgar.isChecked());
    intent.putExtra("buburKacang", buburKacang.isChecked());
    intent.putExtra("kuih", kuih.isChecked());


    Intent nextPage= new Intent(this, doReceipt.class);
    nextPage.putExtra("nog",Integer.parseInt(editText.getText().toString()));


    startActivityForResult(intent,0);



    }  
 } 

这是logcat错误。

  11-07 09:21:14.179: E/AndroidRuntime(281):    at com.example.mobilecatering.mcpakej1.goReceipt(mcpakej1.java:90)

4 个答案:

答案 0 :(得分:1)

传递值如:

Intent nextPage= new Intent(this, NextPage.class);
nextPage.putExtra("myValue",Integer.parseInt(editText.getText().toString()));
startActivity(nextPage);

获取值为:

 Bundle bundle = getIntent().getExtras();
    if(bundle!=null)
    {
        Integer value= bundle.getInt("myValue");
    }

更新

由于您尚未分配要使用的EditText,因此无法解决您获取的错误。使用此代码:

在OnCreate中引用EditText:

 EditText yourEditText;
    public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mcpakej1);

      yourEditText = (EditText) findViewById(R.id.nog);
    }

更新:2

doReceipt.java:

public class doReceipt extends Activity

{


     TextView tvOutput27;

    public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.receipt);


        tvOutput27 = (TextView) findViewById(R.id.textView27);

        if(getIntent().getIntExtra("nog",0)!=0){
              tvOutput27.setText("Number of guest(s) : "+ getIntent().getIntExtra("nog",0));
      }
  }
}

答案 1 :(得分:0)

isChecked()你是什么意思?它应该是:

intent.putExtra("nog", nog.getText().toString());

在你的第二项活动中:

Bundle b = getIntent().getExtras();
if (b != null) {
String nog = b.getString("nog");
//use the nog

答案 2 :(得分:0)

   mButton = (Button)findViewById(R.id.button);
   mEdit   = (EditText)findViewById(R.id.nog);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText", mEdit.getText().toString());
            }
        });

答案 3 :(得分:0)

从EditText获取值并传递给intent:

intent.putExtra("value", nog.getText().toString());

从intent获取值并设置为TextView:

textView27.setText(getIntent().getStringExtra("value"));

您的实际代码是否有效

public class a extends Activity {

    private EditText nogEditText;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mcpakej1);

        nogEditText = (EditText) findViewById(R.id.nog);

    }

    public void goReceipt(View v)
    {
        Intent intent = new Intent(this, doReceipt.class);
        intent.putExtra("nog", nogEditText.getText().toString());
        startActivityForResult(intent,0);
    }
}

public class doReceipt extends Activity {

    private TextView textView27;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receipt);

        textView27 = (TextView) findViewById(R.id.textView27);

        textView27.setText(getIntent().getStringExtra("nog"));

    }

}