java - Android 应用程序背景颜色按钮

标签 java android button

您好,我还有一个关于我的 hello world 应用程序的问题 我想在按下 A 按钮时更改背景,所以我这样做了:

    public void onclick01(View View)  
       {  
           View.setBackgroundColor(Color.GREEN);

       } 

但这会改变按钮的背景颜色,而不是整个应用。


编辑

我还有两个问题。

1) 我将如何设置

View.setBackgroundColor(Color.GREEN);

类似于:

View.setBackgroundColor(Color.RANDOM);

2) 我将如何做同样的事情来改变文本颜色? 像这样的东西:

View.setTextColor(Color.GREEN);?

最佳答案

ma​​in_act.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />

</LinearLayout>

Activity

public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button b1;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_act);
    layout=(LinearLayout)findViewById(R.id.layout);
    blueButton=(Button)findViewById(R.id.b1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
        // TODO Auto-generated method stub
        layout.setBackgroundColor(Color.BLUE);

    }
});
}
}

关于java - Android 应用程序背景颜色按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170640/

相关文章:

java - Jackson 自定义反序列化器在 Spring Boot 中无法工作

java - 记录 Java servlet 操作

java - 在 Java 中使用线程安全集合时,处理并发问题的最佳方法是什么?

java - 替换已弃用的 org.apache.http.conn.scheme.SchemeRegistry、ThreadSafeClientConnManager 类等

android - Kotlin fragment

vb.net - 为什么VB中的按钮热键不显示带下划线?

android - 如何将 SMS 消息存储到 SQLite 数据库?

android - 动态图、饼图、条形图等

WPF 触发器未按预期工作

android - 在android中为我的按钮添加点击效果