如何将选定的日值作为参数传递给另一个活动?

时间:2013-07-29 06:58:57

标签: android

如何将所选按钮值作为参数传递给另一个活动???下面是我的代码,这是屏幕截图http://imgur.com/YkbE8AS我的代码显示日历水平假设用户选择星期三的任何一天它不是固定值是动态值所以我如何将选定的日值与月份和年份传递给另一个活动作为参数????在我的cdoe 7按钮中哪些文本是动态的我想当用户点击任何一天当前月份和年份的值作为参数传递到另一个活动时假设abc.java当按钮mybutton将点击?< / p>

           public class HoyahCalendar extends Activity {
public static int mYear;
public static int currentIndex = -1;
public static int mMonth;
public static int mDay;
public static String[][] a = new String[6][7];
String  January="January";
String  February="February";
String  March="March";
String  April="April";
String  May="May";
String  June="June";
String  Jully="Jully";
String  August="August";
String  September="September";
String  October="October";
String  November="November";
String  December="December";
String  Monthname;

 TextView date_today;
 ImageView last_month;
 ImageView next_month;

 ImageView last_week;
 ImageView next_week;


 Button e00;
 Button e01;
 Button e02;
 Button e03;
 Button e04;
 Button e05;
 Button e06;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getIntent().setAction("Already created");


    date_today = (TextView) findViewById(R.id.date_today);
    last_month = (ImageView) findViewById(R.id.last_month);
    next_month = (ImageView) findViewById(R.id.next_month);
    last_week = (ImageView) findViewById(R.id.last_week);
    next_week = (ImageView) findViewById(R.id.next_week);



    e00 = (Button) findViewById(R.id.e00);
    e01 = (Button) findViewById(R.id.e01);
    e02 = (Button) findViewById(R.id.e02);
    e03 = (Button) findViewById(R.id.e03);
    e04 = (Button) findViewById(R.id.e04);
    e05 = (Button) findViewById(R.id.e05);
    e06 = (Button) findViewById(R.id.e06);

    Calendar mCalendar = Calendar.getInstance();
    mYear = mCalendar.get(Calendar.YEAR);
    mMonth = mCalendar.get(Calendar.MONTH) + 1;
    mDay = mCalendar.get(Calendar.DAY_OF_MONTH);



    last_month.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (mMonth == 1) {
                mYear -= 1;
                mMonth = 12;
                new ShowCalendar(mYear, mMonth);
                showOnScreen();
            } else {
                mMonth -= 1;
                new ShowCalendar(mYear, mMonth);
                showOnScreen();
            }

        }
    });

    next_month.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (mMonth == 12) {
                mYear += 1;
                mMonth = 1;
                new ShowCalendar(mYear, mMonth);
                showOnScreen();
            } else {
                mMonth += 1;
                new ShowCalendar(mYear, mMonth);
                showOnScreen();
            }

        }
    });

    last_week.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (mMonth == 1) {
                mYear -= 1;
                mMonth = 12;
                new ShowCalendar(mYear, mMonth, mDay, "last");
                showOnScreen();
            } else {
                // mMonth -= 1;
                new ShowCalendar(mYear, mMonth, mDay, "last");
                showOnScreen();
            }
        }
    });

    next_week.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (mMonth == 12) {
                mYear += 1;
                mMonth = 1;
                new ShowCalendar(mYear, mMonth, mDay, "next");
                showOnScreen();
            } else {
                if (HoyahCalendar.currentIndex == 4) {
                    HoyahCalendar.currentIndex = 4;
                    // mMonth += 1;
                }

                new ShowCalendar(mYear, mMonth, mDay, "next");
                showOnScreen();
            }

        }
    });

    new ShowCalendar(mYear, mMonth);
    showOnScreen();

  }

       public void showOnScreen() {

    if (mMonth ==1) 
    {
        Monthname="January";
        }
    else 
        if (mMonth ==2) { 
            Monthname="February";
            }

        else
    if (mMonth ==3) { Monthname="March";}
    else 
        if (mMonth ==4) { Monthname="April";    }

        else
    if (mMonth ==5) { Monthname="May";}
    else 
        if (mMonth ==6) { Monthname="June"; }
        else
    if (mMonth ==7) { Monthname="July";}
    else 
        if (mMonth ==8) { Monthname="August";   }
        else
    if (mMonth ==9) { Monthname="September";}
    else 
        if (mMonth ==10)    { Monthname="October";  }
    if (mMonth ==11)    { Monthname="November";}
    else 
        if (mMonth ==12)    { Monthname="December"; }


    date_today.setText(  Monthname + "  " +mYear);
    e00.setText("" + a[0][0]);


    if(e00.getText().toString().equals(String.valueOf(mDay)))



    {e00.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button1 text equals!", Toast.LENGTH_SHORT).show();

    }

    else
    {
        e00.setTextColor(Color.parseColor("#000000"));
    }


    e01.setText("" + a[0][1]);

    if(e01.getText().toString().equalsIgnoreCase (String.valueOf(mDay)))


    {
        e01.setTextColor(Color.parseColor("#FFBBFF"));

    Toast.makeText(this, "Button2 text equals!", Toast.LENGTH_SHORT).show();

    }
    else
    {
        e01.setTextColor(Color.parseColor("#000000"));
    }

    e02.setText("" + a[0][2]);
    if(e02.getText().toString().equals(String.valueOf(mDay)))

    {e02.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button3 text equals!", Toast.LENGTH_SHORT).show();

    }
    else
    {
        e02.setTextColor(Color.parseColor("#000000"));
    }
    e03.setText("" + a[0][3]);
    if(Integer.parseInt(e03.getText().toString()) == mDay)
    {e03.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button4 text equals!", Toast.LENGTH_SHORT).show();

    }


    else
    {
        e03.setTextColor(Color.parseColor("#000000"));
    }


    e04.setText("" + a[0][4]);
    if(e04.getText().toString().equals(String.valueOf(mDay)))
    {e04.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button5 text equals!", Toast.LENGTH_SHORT).show();

    }

    else
    {
        e04.setTextColor(Color.parseColor("#000000"));
    }


    e05.setText("" + a[0][5]);
    if(e05.getText().toString().equals(String.valueOf(mDay)))
    {e05.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button6 text equals!", Toast.LENGTH_SHORT).show();

    }

    else
    {
        e05.setTextColor(Color.parseColor("#000000"));
    }

    e06.setText("" + a[0][6]);
    if(e06.getText().toString().equals(String.valueOf(mDay)))
    {e06.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button7 text equals!", Toast.LENGTH_SHORT).show();

    }


    else
    {
        e06.setTextColor(Color.parseColor("#000000"));
    }


}


 public void onRestart() {
     super.onRestart();
         Intent intent = getIntent();
         finish();
         startActivity(intent); 
         }

3 个答案:

答案 0 :(得分:0)

你可以试试这个:

要在启动新活动时将信息从一个活动传递到另一个活动,请执行以下操作:

Intent top = new Intent(Main.this, New.class);
Bundle b = new Bundle();
b.putString("key1", "value1");
b.putString("key2", "value2");
b.putString("key3", "value3");
top.putExtras(b);
startActivity(top);

然后在新启动的活动中,在onCreate()中输入以下内容:

Bundle b = getIntent().getExtras();
String one = b.get("key1");
String two =b.get("key2");
String three =b.get("key3");

这将使用您提供的密钥获取上一个活动的值。

答案 1 :(得分:0)

好吧,你说你知道如何通过意图传递bundle,为什么不在你的类中创建一个用当前日期初始化的变量。

variable value = current date;

点击任何按钮,您将变量值更改为您要发送到活动的值

onClick(){
value = value you want to send
}

并将该参数以bundle的形式发送给intent。

因此您的要求:

 " if user not click any value then go current date value as parameter else what ever button eser click that day value will go as parameter"

将起作用,如果没有点击任何内容,变量value已经使用当前日期初始化,因此将被发送到活动,您可以按照自己的方式处理它。

Plz忽略语法,它的jst伪代码。

答案 2 :(得分:0)

使用function和变量dtSelect 在开头设置dtSelect = default 如果用户选择任何按钮设置该特定值为dtSelect

一旦你将dtSelect中存储的值传递给function(作为i.putstring()),就可以根据你在变量中的值来执行你想要的任务。