我如何将数据从一个活动传递到另一个活动

时间:2015-10-03 20:41:31

标签: java android eclipse android-intent android-activity

package com.example.assignmentone;

import java.util.ArrayList;

import android.R.integer;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.text.InputType;

import android.text.method.DigitsKeyListener;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.AlphabetIndexer;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TableRow.LayoutParams;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {


EditText et;
static TableLayout tl;
Intent intent;
Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et=(EditText) findViewById(R.id.et);
    //et2=(EditText) findViewById(R.id.et2);
    tl=(TableLayout) findViewById(R.id.tl);
    btn2=(Button) findViewById(R.id.btn2);


}
public void get(View v)
{
    startActivity(intent);

}

public void doo(View v)
{
    if(et.getText().toString().isEmpty()||Integer.parseInt(et.getText().toString())==0)

    {
        Toast.makeText(this, "Please Enter Valid Row #", Toast.LENGTH_SHORT).show();

        //Toast.makeText(this, et2.getInputType()+"", Toast.LENGTH_SHORT).show();

    }else {
        btn2.setEnabled(true);

        power(Integer.parseInt(et.getText().toString()));


    }
}
public void power(int r){
    //r=Integer.parseInt(et.getText().toString());
    //ArrayList<View>[][] table1 = new ArrayList[r][4];
    tl.removeAllViews();
    for(int i=0;i<=r;i++)
    {
        TableRow tRow=new TableRow(this);
        tRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        for(int j=0;j<4;j++)
        {
            if(i==0)
            {
                TextView tv=new TextView(this);
                tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                if(j==0)
                {
                    tv.setKeyListener(DigitsKeyListener.getInstance(" abcdef:-.")); 

                    tv.setText("Name");
                    tRow.addView(tv);
                }

                else if(j==1)
                {
                    tv.setText("Subject 1");
                    tRow.addView(tv);
                }
                else if(j==2)
                {
                    tv.setText("Subject 2");
                    tRow.addView(tv);
                }
                else if( j==3)
                {
                    tv.setText("Subjct 3");
                    tRow.addView(tv);

                }

            }
            else if(i>0)
            {
                EditText temp=new EditText(this);
                temp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                if(j==0)
                    {

                        temp.setInputType(1);
                        tRow.addView(temp);



                    }else if(j==1)
                    {
                        temp.setInputType(4098);
                        tRow.addView(temp);

                    }
                    else if(j==2)
                    {
                        temp.setInputType(4098);
                        tRow.addView(temp);

                    }
                    else if( j==3)
                    {
                        temp.setInputType(4098);
                        tRow.addView(temp);

                    }                   
            }


        }
        tl.addView(tRow);
    }


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

您可以使用putExtra和getExtra将活动中的数据发送到另一个。使用此方法,您可以发送字符串Serializable对象...等。 请参阅此example以发送字符串,此example用于发送对象