【问题标题】:how to pass selected day value as parameter to another activity?如何将选定的日期值作为参数传递给另一个活动?
【发布时间】:2013-07-29 06:58:56
【问题描述】:

如何将选定的按钮值作为参数传递给另一个活动???下面是我的代码,这是屏幕截图http://imgur.com/YkbE8AS 我的代码水平显示日历假设用户选择一周中的任何一天,例如星期三,它的不固定值是动态值所以我如何将选定的日期值与月份和年份传递给另一个活动参数??? >

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

【问题讨论】:

  • 在开始新活动的意图时将这一天作为一个额外的整数。
  • 您可以使用 Intent().putExtra(name, value) 在活动之间传递数据

标签: android


【解决方案1】:

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

variable value = current date;

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

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 伪代码。

【讨论】:

  • 我知道如何通过,但我不知道用户点击了哪个按钮,水平方向有 7 个按钮我如何知道用户点击了哪个按钮?它的值将作为参数传递??
  • 首先,您的代码中没有显示按钮单击的 OnClicklistener\,如果您可以发布代码,那么如果您对所有按钮使用通用的点击侦听器,那么您可以区分哪个按钮是从它们的 id 中单击的,使用带有按钮的所有 id 的开关作为案例,并与 view.getID() 进行比较并更新值变量
  • 我不想为每个按钮添加 Intent 7 次,我希望单个 Intent 用于将按下的按钮值作为参数传递
  • 是的,在您的类中实现 onCLicklistener 并在 onClick() 函数中使用带有所有按钮 ID 的 switch 语句,并将此侦听器设置为所有按钮。您可以在单个函数中执行此操作,只需使用 switch(view.getID()) 区分其 id 上的按钮
  • 这将有助于检查第二个答案。 stackoverflow.com/questions/3795439/…
【解决方案2】:

你可以试试这个:

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

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

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

【讨论】:

  • 知道如何将值作为参数传递,但在我的代码中如何获取选定的按钮值??????我想如果用户不点击任何值,然后将当前日期值作为参数,否则任何按钮 eser 单击该日期值将作为参数我该怎么做????
  • 您可以创建一个Class变量来存储点击按钮的日期。所以你可以将它传递给意图。
  • 创建任何全局变量,并在单击按钮时将值设置为 BtnClicked = 2 或 BtnClicked = btn.tag 等变量。当您发送到另一个活动时,请传递该变量。
【解决方案3】:

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

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

【讨论】:

  • 我不相信用所有语法编写确切的代码......(我讨厌记住)..但是有伪代码我认为我们可以使用 android 文档编写语法......还有一个提示可能正在使用 switch 语句...
  • 我不会在每个按钮上生成意图函数,我只是希望当用户单击任何日期时,当用户单击新按钮(如 mybutton 其 str Intent 并将所选按钮值作为参数传递)时,该日期值将保存
  • 我的意思是当我的按钮点击时如何存储选定的日期值并作为参数传递给另一个活动???
  • 好的,我有一个基本问题是你能把选定的日期存储到一个变量中吗.....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多