Android Studio通过单击1按钮多次更改背景颜色

时间:2017-02-21 20:01:08

标签: java android

我是Android Studio的新手,正在尝试弄清楚如何更改启动应用的背景颜色。

应用程序加载的那一刻,我在屏幕上看到一个按钮,当我点击它时,它会变为红色。

我想要的是当你点击按钮时,它从红色变为绿色变为蓝色而不是变回红色。

但是,我一直收到这些错误:

  

错误:任务':app:compileDebugJavaWithJavac'执行失败。   编译失败;请参阅编译器错误输出以获取详细信   错误:(72,9)错误:类,接口或枚举预期

主要活动XML文件:

com.apple.AddressBook

测试活动Java代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Color"
    android:onClick="onChangeColor"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

为了给你一个很好的帮助,有必要看你的代码。 无论如何,如果我对你说得对,也许这会对你有所帮助:

在xml布局上:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Color"
    android:onClick="onChangeColor"/>
</LinearLayout>

关于您的活动:

private int colorIndex = 1;
public void onChangeColor(View view) {
    int color;
    if(colorIndex==0) {
        color = Color.RED;
        colorIndex = 1;
    }else if(colorIndex==1) {
        color = Color.GREEN;
        colorIndex = 2;
    }else {
        //colorIndex = 2
        color = Color.BLUE;
        colorIndex = 0;
    }

    View layout = findViewById(R.id.layout);
    layout.setBackgroundColor(color);
}

活动中的onCreate

   View layout = findViewById(R.id.layout);

    layout.setBackgroundColor(Color.RED);

答案 1 :(得分:0)

如果我理解你想要的是什么,随着时间的推移过渡一系列颜色,每种颜色持续1-2秒。您可以使用Android的默认CountDownTimer

保持xml布局相同。

在您的活动中:

var mysql      = require('mysql');
var fs = require('fs');
var configFile = process.cwd() + "\\config.json";
var configFileContents = fs.readFileSync(configFile, 'utf8');
var config = JSON.parse(configFileContents);
//config = config[process.env.BLENV];
console.log(config.PROD.HTTP);

var connection = mysql.createConnection({
  host     : config.QA.SQLHostName,
  user     : config.QA.SQLUserName,
  password : config.QA.SQLPassword,
  database : config.QA.SQLDatabase,
  multipleStatements: true
});

connection.connect();

var timeInMs = Date.now();
console.log(timeInMs);

connection.query(` 
select 
c.name as "Name",
cl.u_geographic_region as "Region",
CASE
    WHEN cic.os like '%Windows%' THEN 'Windows'
    WHEN cic.os like '%aix%' THEN 'AIX'
    WHEN cic.os like '%esx%' THEN 'ESX'
    WHEN cic.os like '%linux%' THEN 'Linux'
    WHEN cic.os like '%solaris%' THEN 'Solaris'
        ELSE 'UNKNOWN'
    END as "Operating System",

ci.dns_domain as "Host domain",
ci.ip_address as "IP Address",
CASE
    WHEN c.sys_class_name = 'cmdb_ci_computer' THEN 'Computer'
    WHEN c.sys_class_name = 'cmdb_ci_win_server' THEN 'Windows Server'
    WHEN c.sys_class_name = 'cmdb_ci_esx_server' THEN 'ESX Server'
    WHEN c.sys_class_name = 'cmdb_ci_aix_server' THEN 'AIX Server'
    WHEN c.sys_class_name = 'cmdb_ci_lb' THEN 'Load Balancer'
    WHEN c.sys_class_name = 'cmdb_ci_lb_ace' THEN 'lb_ace'
    WHEN c.sys_class_name = 'cmdb_ci_aix_lb_bigip' THEN 'lb_bigip'
    WHEN c.sys_class_name = 'cmdb_ci_linux_server' THEN 'Linux Server'
    WHEN c.sys_class_name = 'cmdb_ci_mainframe' THEN 'IBM Mainframe'
    WHEN c.sys_class_name = 'cmdb_ci_solaris_server' THEN 'Solaris Server'
    WHEN c.sys_class_name = 'cmdb_u_ci_ibmi_server' THEN 'IBMi Server'
    WHEN c.sys_class_name = 'cmdb_ u_ci_ibmi_server' THEN 'IBMi Server'
    WHEN c.sys_class_name = 'cmdb_u_server_appliance' THEN 'ServerAppliance'
        ELSE 'Unknown'
    END as "Class",

CASE 
    WHEN c.install_status = '1' THEN 'Installed'
    WHEN c.install_status = '100' THEN 'Absent'
    WHEN c.install_status = '2' THEN 'On Order'
    WHEN c.install_status = '3' THEN 'In Maintenance'
    WHEN c.install_status = '4' THEN 'Pending Install'
    WHEN c.install_status = '5' THEN 'Pending Repair'
    WHEN c.install_status = '6' THEN 'In Stock'
    WHEN c.install_status = '7' THEN 'Retired'
    WHEN c.install_status = '8' THEN 'Stolen'
    WHEN c.install_status = '9' THEN 'In Use'
                         ELSE 'Unknown'
          END As "lifecycle Status"

from
cmdb as c
join cmdb_ci ci on c.sys_id = ci.sys_id
join cmdb_ci_hardware as cih on c.sys_id = cih.sys_id
join cmdb_ci_computer cic on c.sys_id = cic.sys_id
join cmn_location as cl on cl.sys_id = c.location
where c.name is not null
`, function(err, rows, fields) {
  if (err) throw err;

 for (var i in rows) {
  console.log('the rows are', rows[i]);
}
});

connection.end();

希望这可能会有所帮助。如果我误解了什么,请发表评论。

编辑:我将视图转换为LinearLayout