如何将setText()移动到Android Studio上的新活动?

时间:2016-03-10 10:04:56

标签: java android button android-activity settext

我想要制作一个篮子,并希望根据按下的按钮,屏幕上显示的价格然后出现在TextView09(表格中的部分)的篮子中(新活动),但此时出现的是什么是android.support.v7.widget.App ...

以下是该产品的活动页面:

package com.example.emily.woodensigns;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.LayoutInflater;


public class Letters extends AppCompatActivity {
    int txtSize = 14;
    EditText Wood;
    Button bSize, bSize1, bSize2, bSize3, bBasket;
    public int count = 5;
    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_letters);

        Wood = (EditText) findViewById(R.id.Wood);
        Button bSize = (Button) findViewById(R.id.bSize);
        Button bSize1 = (Button) findViewById(R.id.bSize1);
        Button bSize2 = (Button) findViewById(R.id.bSize2);
        Button bSize3 = (Button) findViewById(R.id.bSize3);
        Button bBasket = (Button) findViewById(R.id.bBasket);
        final TextView price = (TextView) findViewById(R.id.price);


        bSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Wood.setTextSize(40);
                price.setText("£10");
                TextView price = (TextView) findViewById(R.id.price);
                String pri = price.getText().toString();
                Intent intent2 = new Intent(Letters.this, Letters.class);
                intent2.putExtra("£",pri);

            }
        });
        bSize1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Wood.setTextSize(60);
                price.setText("£20");
            }
        });
        bSize2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Wood.setTextSize(100);
                price.setText("£35");
            }
        });
        bSize3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Wood.setTextSize(150);
                price.setText("£50");
            }
        });


        bBasket.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText Wood = (EditText) findViewById(R.id.Wood);
                String str = Wood.getText().toString();
                if (str.length() == 0) {
                    Wood.requestFocus();
                    Wood.setError("FIELD CANNOT BE EMPTY");
                } else if (str.length() >= 2) {
                    Wood.requestFocus();
                    Wood.setError("You can only type one letter!");
                } else {
                   Intent intent2 = new Intent(Letters.this, Basket.class);
                    intent2.putExtra("MY_INFO",str + " - Letters" + price );
                    startActivity(intent2);
                }
            }
        });
    }
}

篮子的活动:

package com.example.emily.woodensigns;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;


public class Basket extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_basket);

        TextView TextView08 = (TextView) findViewById(R.id.TextView08);
        TextView TextView09 = (TextView) findViewById(R.id.TextView09);
        Button bCheckout=(Button) findViewById(R.id.bCheckout);

        String strFromActivity = getIntent().getStringExtra("MY_INFO");
        TextView08.setText(strFromActivity);

        String priFromActivity = getIntent().getStringExtra("£" );
        TextView09.setText(priFromActivity);
        Toast.makeText(getBaseContext(),priFromActivity, Toast.LENGTH_LONG).show();

        bCheckout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent2 = new Intent(Basket.this, customerDetails.class);
                startActivity(intent2);
            }
        });
    }
}

购物篮xml文件:

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="259dp"
    android:shrinkColumns="*"  android:stretchColumns="*" android:background="#ffffff">


    <!-- Row 1 with single column -->
    <TableRow
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="center_horizontal">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:text=""
            android:layout_span="3"
            android:padding="18dip"
            android:background="#b0b0b0"
            android:textColor="#000"/>


    </TableRow>

    <!-- Row 2 with 3 columns -->


    <TableRow
        android:id="@+id/tableRow1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <TextView
            android:id="@+id/TextView04"
            android:layout_weight="1"
            android:background="#dcdcdc"
            android:textColor="#000000"
            android:padding="20dip"
            android:gravity="left"/>
        <TextView
            android:id="@+id/TextView05"
            android:text="Product"
            android:layout_weight="1"
            android:background="#dcdcdc"
            android:textColor="#000000"
            android:padding="20dip"
            android:gravity="center"/>

        <TextView
            android:id="@+id/TextView06"
            android:text="Price"
            android:layout_weight="1"
            android:background="#d3d3d3"
            android:textColor="#000000"
            android:padding="20dip"
            android:gravity="center"/>



    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/TextView07"
            android:layout_weight="1"
            android:background="#dcdcdc"
            android:textColor="#000000"
            android:padding="20dip"
            android:gravity="left"/>
        <TextView
            android:id="@+id/TextView08"
            android:text="Product"
            android:layout_weight="1"
            android:background="#dcdcdc"
            android:textColor="#000000"
            android:padding="20dip"
            android:gravity="center"/>

        <TextView
            android:id="@+id/TextView09"
            android:text="Price"
            android:layout_weight="1"
            android:background="#d3d3d3"
            android:textColor="#000000"
            android:padding="20dip"
            android:gravity="center"/>
    </TableRow>

</TableLayout>

谢谢

1 个答案:

答案 0 :(得分:0)

您的btn点击代码如下所示:

bBasket.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText Wood = (EditText) findViewById(R.id.Wood);
                String str = Wood.getText().toString();
                if (str.length() == 0) {
                    Wood.requestFocus();
                    Wood.setError("FIELD CANNOT BE EMPTY");
                } else if (str.length() >= 2) {
                    Wood.requestFocus();
                    Wood.setError("You can only type one letter!");
                } else {
                   Intent intent2 = new Intent(Letters.this, Basket.class);
                    intent2.putExtra("MY_INFO",str + " - Letters" + price );
                    TextView price = (TextView) findViewById(R.id.price);
                    String pri = price.getText().toString();
                    intent2.putExtra("£",pri);
                    startActivity(intent2);
                }
            }
        });

希望这会对你有所帮助。