2013-07-29 43 views
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); 
     } 
+1

開始了新的活動的意圖時傳遞一天,一個整型附加。 – kabuto178

+0

您可以使用意向()。putExtra(名稱,值)活動之間傳遞數據 –

回答

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已經initlialized與當前日期因此將被髮送到活動,你可以處理它,你想要的方式。

PLZ忽略語法,其JST僞代碼。

+0

我知道如何通過,但我不知道什麼按鈕用戶點擊有7按鈕水平如何知道哪個按鈕用戶點擊?它的值將作爲參數傳遞? – user2589245

+0

首先,有沒有在您的按鈕,點擊\代碼所示OnClicklistener,如果你能發佈的代碼,然後可以幫助另外,如果你使用的是常見的點擊收聽所有按鈕,然後就可以區分它們的ID哪個按鈕被點擊,使用帶有按鈕的所有ID的交換機的情況下,用view.getID(比較)以及更新變量的值 – Neji

+0

我張貼以上 – user2589245

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

這將得到從以前的值使用您提供的密鑰進行活動。

+0

iknow怎麼傳值參數,但在我的代碼我怎麼選擇按鈕值?????我想如果用戶不單擊任何值,然後將當前日期值作爲參數其他按鈕eser單擊該日值將作爲參數如何做這? – user2589245

+0

您可以創建一個存儲點擊按鈕日的類變量。所以你可以把它傳遞給意圖。 –

+0

怎麼樣?告訴我怎麼樣? – user2589245

0

化妝使用function和可變dtSelect 的設定dtSelect = default開頭 ,並且如果用戶選擇任何按鈕的特定值設置爲dtSelect

一旦u必須存儲在dtSelect通的值到一個function(爲i.putstring())開展任務ü願望根據您在該變量具有值...

+0

告訴我實際上請沒有明確的書面 – user2589245

+1

我不相信編寫確切的代碼與所有語法...(我討厭背誦理解)..但有僞代碼,我想我們可以使用Android文檔編寫語法...一個更暗示可以使用switch語句... –

+0

是U能夠區分7次按鈕點擊不同.... –

相關問題