从android中动态创建的textview获取价值

时间:2014-08-06 07:21:34

标签: android eclipse

MainActivity的代码

如何从文本框中获取值,该用户的数量框将输入值。价格将来自服务器,而不是我想计算账单并在下一个布局中显示。请给我一些简单的解释,因为我不是专家。提前谢谢。

MainActivity .java

package com.example.myone;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
     EditText textIn;
     Button buttonAdd;
     LinearLayout container;
     private Button done;
     private TextView tv;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              textIn = (EditText)findViewById(R.id.textin);
              buttonAdd = (Button)findViewById(R.id.add);
              container = (LinearLayout)findViewById(R.id.container);
              done = (Button) findViewById(R.id.cal);
              tv= (TextView) findViewById(R.id.tv);

             buttonAdd.setOnClickListener(new OnClickListener(){

              @Override
               public void onClick(View arg0) {
                LayoutInflater layoutInflater = 
                  (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row, null);
                TextView textOut = (TextView)addView.findViewById(R.id.textout);
                textOut.setText(textIn.getText().toString());

                //(int1);

                Button buttonRemove = (Button)addView.findViewById(R.id.remove);
                buttonRemove.setOnClickListener(new OnClickListener(){

                 @Override
                 public void onClick(View v) {
                  ((LinearLayout)addView.getParent()).removeView(addView);
                 }});
                container.addView(addView);
               }});

      }

}

MainActivity.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@drawable/mainbg"
    tools:context=".MainActivity" >


  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/textin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Enter your item here"
            />
        <Button 
            android:id="@+id/cal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_below="@+id/textin"
            android:text="Done!"
            android:layout_alignParentLeft="true"
            />
        <Button 
            android:id="@+id/add"
            android:text="Add"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignBaseline="@+id/cal"
            />

    </RelativeLayout>
    <LinearLayout 
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>

</LinearLayout>

Row.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@drawable/borderbg"
    android:orientation="horizontal"
    android:id="@+id/rel"
    android:layout_height="wrap_content">

    <TextView 
        android:id="@+id/textout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="Item"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
                />

    <EditText
        android:id="@+id/qty"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignTop="@+id/price"
        android:layout_toRightOf="@+id/price"
        android:layout_marginLeft="50dp"
        android:textSize="12sp"
        android:hint="Qty" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textout"
        android:layout_alignBottom="@+id/textout"
        android:layout_marginLeft="42dp"
        android:layout_toRightOf="@+id/textout"

        android:hint="Price" />

    <Button
        android:id="@+id/remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="left"
        android:text="remove" />

</RelativeLayout>

我只想创建一个包含20个产品的基本购物应用。价格和产品名称将来自服务器,数量将由用户输入,那我该怎么做?

2 个答案:

答案 0 :(得分:5)

您正在寻找类似的东西

int count = container.getChildCount(); 
for (int i = 0; i < count; i++) {
   final View row = container.getChildAt(i);
   TextView textOut = (TextView)row.findViewById(R.id.textout);
   String data = textOut.getText().toString();
}

container是您添加了充气行的LinearLayout

答案 1 :(得分:3)

试试这个: -

从文本框中获取用户输入: -

int qty = Integer.parseInt(qtyTextBox.getText().toString());

Caluculate bill: -

double bill =  price*qty;

将账单价值发送到下一个活动: -

Intent in = new Intent();
in.putExtra("Bill", bill);
startActivity(in);

在下一个活动中获取账单价值: -

Intent in = getIntent();
int mBill = in.getExtra("Bill");
string bill = String.valueOf(mBill);

现在您可以设置&#39; bill&#39;到要显示它的TextView。