【问题标题】:Start activity and change the widget image button on click in android在android中单击时开始活动并更改小部件图像按钮
【发布时间】:2015-02-12 07:51:26
【问题描述】:

我想启动一个活动并在点击时更改小部件图像按钮。 我该怎么做?我不是很清楚。经过谷歌搜索,仍然无法解决。

【问题讨论】:

  • 到目前为止您尝试了什么?任何代码有错误?

标签: android-widget


【解决方案1】:

创建按钮有多种方法。
我将使用编程方式:
在一个 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);

请记住,每个活动都有自己的变量和视图,如果您不传递按钮的状态或新活动的颜色,则不会有这些信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2013-11-28
    相关资源
    最近更新 更多