启动活动并在android中单击更改小部件图像按钮

时间:2015-02-12 07:51:27

标签: android-widget

我想在点击时启动活动并更改小部件图片按钮。 我该怎么做?我不太了解它。经过大量的谷歌搜索,它仍然无法解决。

1 个答案:

答案 0 :(得分: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);

请记住,每个活动都有自己的变量和视图,如果你没有通过新活动的按钮状态或颜色,这将不具备这些信息。