0

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

           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); 
         }
4

3 回答 3

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");

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

于 2013-07-29T07:04:27.060 回答
0

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

variable value = current date;

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

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

并将该参数捆绑发送到意图。

因此您的要求:

 " 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已使用当前日期初始化,因此将发送到活动,您可以按照您想要的方式处理它。

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

于 2013-07-29T07:10:58.580 回答
0

在开头使用 afunction和一个变量dtSelect 集,如果用户选择任何按钮,则将该特定值设置为 dtSelectdtSelect = default

一旦您存储了一个值,dtSelect将其传递给function(as i.putstring()) 以根据您在变量中的值执行您想要的任务...

于 2013-07-29T07:12:00.793 回答