显示另一个活动的值

时间:2017-04-28 06:34:00

标签: java android textview

我想将btn_save上的Income.java的textview1的值传递给MainActivity。并且还希望在再次传递新值时总结MainActivity.java的值请帮助我...

Income.java

public class Income extends AppCompatActivity implements AdapterView.OnItemSelectedListener,Calculator.OnDialogReturnListener,View.OnClickListener{

Spinner spinner1;
String[] name,name1;
Button  btn_cancel, btn_save,acc_btn,button,updatebtn;
TextView  textView1,textView2,accountTV,wantupdate,income;
//EditText textView1;
int yy,mm,dd;
private Income_class i_class;
DatabaseHandler mydb;

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

    textView1 = (TextView) findViewById(R.id.income_tv);
    textView2 = (TextView) findViewById(R.id.date_tv);
    accountTV = (TextView) findViewById(R.id.acc_tv);
    income = (TextView) findViewById(R.id.incometv);
    wantupdate = (TextView) findViewById(R.id.notupdate);
    btn_cancel = (Button) findViewById(R.id.cancel_btn);
    updatebtn = (Button) findViewById(R.id.up_btn);
    acc_btn = (Button) findViewById(R.id.account_btn);
    btn_save = (Button) findViewById(R.id.save_btn);
    button = (Button) findViewById(R.id.btn);

     final AlertDialog.Builder dialog = new AlertDialog.Builder(Income.this);
     final AlertDialog.Builder dialog1 = new AlertDialog.Builder(Income.this);
     final AlertDialog.Builder dialog2 = new AlertDialog.Builder(Income.this);
     final AlertDialog.Builder dialog3 = new AlertDialog.Builder(Income.this);

    final EditText editText = new EditText(Income.this);
    dialog.setView(editText);

    final DatabaseHandler db = new DatabaseHandler(Income.this);
    mydb = new DatabaseHandler(this);

    button.setOnClickListener(this);

    // for calculator
    final Calculator cdd = new Calculator(Income.this);
    cdd.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    //cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
    cdd.setOnDialogReturnListener(this);

    cdd.show();

    // for displaying date
    final Calendar c = Calendar.getInstance();
    yy = c.get(Calendar.YEAR);
    mm = c.get(Calendar.MONTH);
    dd = c.get(Calendar.DAY_OF_MONTH);

    //set current date into text..
    textView2.setText(new StringBuilder()
            //month os 0 based. Just add 1
            .append(dd).append("-").append(mm + 1).append("-").append(yy));

    btn_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Income.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    });

    acc_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Income.this,Account_detail.class);
            startActivity(intent);
            finish();
        }
    });
    updatebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String cat = button.getText().toString();
            String acc = accountTV.getText().toString();
            String text = textView1.getText().toString();

            if(text.equals("0")){
                dialog3.setTitle("You must enter amount.");

                dialog3.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface ialog, int which) {

                    }
                });
                dialog3.show();
            }
            else if(cat.equals("Select Category")){
                dialog1.setTitle("Please Select category");

                dialog1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /*Intent intent = new Intent(Income.this, Income.class);
                        startActivity(intent);
                        finish();*/
                        button.requestFocus();
                    }
                });
                dialog1.show();
            }
            else if(acc.equals("")){
                dialog2.setTitle("Please Select Account");

                dialog2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        acc_btn.requestFocus();
                    }
                });
                dialog2.show();
            }

            else {

                String value = accountTV.getText().toString();
                String value2 = textView1.getText().toString();
                String value3 = button.getText().toString();
                db.update_income(value, value2, value3);

                Toast.makeText(getBaseContext(), "Updated Successfully...", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Income.this, Acc_income.class);
                startActivity(intent);
                finish();
            }
        }
    });
    btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String cat = button.getText().toString();
            String acc = accountTV.getText().toString();
            String text = textView1.getText().toString();

            if(text.equals("0")){
                dialog3.setTitle("You must enter amount.");

                dialog3.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface ialog, int which) {

                    }
                });
                dialog3.show();
            }
            else if(cat.equals("Select Category")){
                dialog1.setTitle("Please Select category");

                dialog1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /*Intent intent = new Intent(Income.this, Income.class);
                        startActivity(intent);
                        finish();*/
                        button.requestFocus();
                    }
                });
                dialog1.show();
            }
            else if(acc.equals("")){
                dialog2.setTitle("Please Select Account");

                dialog2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        acc_btn.requestFocus();
                    }
                });
                dialog2.show();
            }
            else {

                String balance2 = textView1.getText().toString();
                String name = accountTV.getText().toString();
                String category = button.getText().toString();
                db.income_insert(name, category, balance2);

                Toast.makeText(getBaseContext(), "Account Added...",
                        Toast.LENGTH_SHORT).show();


            }

        }
    });
    wantupdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder dialog = new AlertDialog.Builder(Income.this);
            dialog.setTitle("Enter Income");

            final EditText editText = new EditText(Income.this);
            dialog.setView(editText);

            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    String value1 = textView1.getText().toString();
                    String value2 = editText.getText().toString();

                    if(textView1.equals("0")){
                        String a = editText.getText().toString();
                        textView1.setText(a);
                    }
                    else if(value2.equals("")){
                        Toast.makeText(Income.this,"Please Enter Amount.",Toast.LENGTH_LONG).show();
                    }
                    else {

                        int a = Integer.parseInt(value1);
                        int b = Integer.parseInt(value2);
                        int sum = a + b;
                        textView1.setText(Integer.toString(sum));
                    }
                }
            });
            dialog.show();
        }
    });

    Intent intent = getIntent();
    accountTV.setText(intent.getStringExtra("ppp"));
}
public void addnumber(){


}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String name2 = name[position];

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

@Override
public void onReturn(String data) {

    textView1.setText(data);

}
@Override
public void onClick(View v) {

    /*String cat = button.getText().toString();

    if(cat.equals("Select Category")){
        button.setError("Please Select category");
        button.requestFocus();
    }
    else */


        final CharSequence[] items = {"Automobile", "Entertainment", "Family", "Food And Drinks", "Gasoline", "Gifts And Donations",
                "Groceries", "Health And Fitness", "Housing", "Medical", "Other", "Parking", "Shopping", "Utilities"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Make your selection");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                button.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(Income.this,MainActivity.class);
    startActivity(intent);
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button expance,income,account,budget,calander,report,more,transfer;
TextView incomeTv;

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

    incomeTv = (TextView)findViewById(R.id.incometv);
    expance = (Button)findViewById(R.id.button_expanse);
    income = (Button)findViewById(R.id.button_income);
    account = (Button)findViewById(R.id.button_account);
    budget = (Button)findViewById(R.id.button_budget);
    transfer = (Button)findViewById(R.id.button_moneytransfer);
    calander = (Button)findViewById(R.id.button_calander);
    report = (Button)findViewById(R.id.button_report);
    more = (Button)findViewById(R.id.button_more);

    expance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Expense.class);
            startActivity(intent);
            finish();
        }
    });

    income.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Income.class);
            startActivity(intent);
            finish();
        }
    });
    account.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Account_detail.class);
            startActivity(intent);
            finish();
        }
    });
    transfer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Account_Transfer.class);
            startActivity(intent);
            finish();

        }
    });
    budget.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Acc_income.class);
            startActivity(intent);
            finish();

        }
    });
    calander.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Account_detail_2.class);
            startActivity(intent);
            finish();

        }
    });
    more.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this,Update.class);
            startActivity(intent);
            finish();

        }
    });

}

1 个答案:

答案 0 :(得分:0)

您可以使用

intent.putExtra("key", value);