2015-02-12 81 views

回答

0

有不同的方法來創建一個按鈕。
我會用編程的方法:
源來創建一個Activity類的按鈕:

//making the container for the button 
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT); 
TableLayout tableLayout = new TableLayout(this); 
tableLayout.setLayoutParams(tableParams); 

//making the button 
Button button = new Button(this); 

//making the class what will handler the click 
button.setOnClickListener(new ClassWithBehavior()); 

//set the color of button state1 
button.getBackground().setColorFilter(Color.parseColor("#ff00ff"), PorterDuff.Mode.MULTIPLY); 

//adding the button at the container 
tableLayout.addView(button); 

的ClassWithBehavior來源:

private class ClassWithBehavior implements View.OnClickListener { 
    public void onClick(View button) { 
     //set the color of button state2 
     this.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.MULTIPLY); 
    } 
} 

創建一個新的活動:

//need have first a Intent before change of the activity 
Intent i = new Intent(a, Activity2.class); 

//share data between old_activity and the new_activity 
i.putExtra("id_tag_name_of_the_data_from_activity1_to_activity2",data); 

//launch the new activity 
this.startActivity(i); 

請記住,每個活動都有自己的變量和視圖,如果你不通過狀態但噸或新活動的顏色,這不會有這些信息。