首页 \ 问答 \ 如何在Android上更改textColor即使在Android中?(How to change textColor on press Even in Android?)

如何在Android上更改textColor即使在Android中?(How to change textColor on press Even in Android?)

如果按下文本视图,是否可以更改文本视图的文本颜色? 我希望实现闪烁效果,只要按下按钮,颜色变化就会持续。

我知道如何使用选择器列表和正确的状态更改textview的背景但是如果用户按下按钮或简单的textview,我该如何更改文本的颜色?


Is it possible to change the text color of a textview if the textview is pressed? I want to achieve a flashing effect with the color change only lasting as long as the button is pressed.

I know how to change the background of the textview with a selector list and the correct state but how can I change the color of text if the user pushes a button or a simple textview?


原文:https://stackoverflow.com/questions/3246819
更新时间:2021-09-14 18:09

最满意答案

你的初始状态可能没有设置,所以this.state.cities实际上是空的。

在渲染函数中做这样的事情:

render() {
  const options = ( this.state.cities || [] ).map( (city) => ( {
      value: city.name,
      label: city.id
  } );

  return (
    -- other components --

    { options.length ? (
        <Field name="city"
            label="Cidade"
            value={this.state.value}
            onChange={this.handleChange}
            options={options}
            component="select"
        />
      ) : null }

    -- other components --
  );
}

( this.state.cities || [] )将检查this.state.cities是否可用,否则它使用一个空数组。


更多细节:你的axios调用是异步的。 这意味着,React不会等待axios获取数据,而只是尝试渲染某些东西。

你的状态尚未设置(可能),因此你会得到这个错误。


Your initial state might not be set, so this.state.cities is actually empty.

Do something like this in your render function:

render() {
  const options = ( this.state.cities || [] ).map( (city) => ( {
      value: city.name,
      label: city.id
  } );

  return (
    -- other components --

    { options.length ? (
        <Field name="city"
            label="Cidade"
            value={this.state.value}
            onChange={this.handleChange}
            options={options}
            component="select"
        />
      ) : null }

    -- other components --
  );
}

( this.state.cities || [] ) will check if this.state.cities is available, otherwise it uses an empty array.


A little more detail: Your axios call is asynchronous. That means, that React doesn't wait for axios to fetch your data but instead just tries to render something.

Your state has not been set (probably) and therefore you get this error.

相关问答

更多
  • 你的初始状态可能没有设置,所以this.state.cities实际上是空的。 在渲染函数中做这样的事情: render() { const options = ( this.state.cities || [] ).map( (city) => ( { value: city.name, label: city.id } ); return ( -- other components -- { options.length ? ( < ...
  • 在你的服务器路由中,永远不会返回给客户端,因为总是定义了SID和TOKEN (至少在你的例子中)。 为确保请求不会失败,您需要在Twilio请求后至少发送一些响应,例如: client.messages .create({ to: req.body.phonenumber, from: SENDER, body: 'This is the ship that made the Kessel Run in fourteen parsecs?' }) .then(messag ...
  • 如果您的意图是使用此Action Creator根据多个先前异步请求的结果分派操作,则不应使用return关键字从异步操作返回Promise。 删除内部return关键字并允许.catch和.catch 分配您的操作。 相关的,我建议你调查一下Async / Await的使用。 这个代码构造(和解释难度)正是为什么Async / Await被放入语言中的原因。 以下代码(经过修改以满足您的架构)将满足您的使用案例。 请注意,我通过嘲讽方法等axios获得了自由。例如,像fetch() , axios方法返回 ...
  • 好的发现了它。 根本不需要映射步骤,数组已经在res中返回。 更换: const posts = res.data.map(obj => obj.data); this.setState({posts}); 有: if (res && res.data) { this.setState({ posts: res.data }); } Ok found it. You don't need the map step at all, the array is already ...
  • 我已经使用异步/等待模式来实现您的要求如下: loadData: async function() { const response = await axios.get( "http://api.football-data.org/v1/competitions/467/fixtures/?timeFrame=p1", config ); this.todaysMatches = response.data; let arr = this.todaysMatches.fi ...
  • 通过在标题中添加Content-Type进行修复 axios({ method: 'PUT', url, data: JSON.stringify(req.body), headers:{'Content-Type': 'application/json; charset=utf-8'} }) .t ...
  • 你不能(或者至少真的不应该)使它同步,所以你需要一种不同的前进方式。 一个想法:从Axios返回承诺: checkUniqueness () { return axios.get('/api/persons/unique/alias', { params: { id: this.id, alias: this.alias, } }) .then((response) => { consol ...
  • 我将轮询逻辑外推到一个单独的函数中: //expects fn() to throw if it failed //if it runs out of retries, poll() will resolve to an rejected promise, containing the latest error function poll(fn, retries = Infinity, timeoutBetweenAttempts = 1000){ return Promise.resolve() ...
  • 最有可能使用Axios的具体类而不是Angular的Http具体类不是一种有效的方法,因为每个类都有不同的接口。 你需要 npm install @ types / axios 在typescript中导入任何其他第三方库时导入axios 请注意,建议使用Angular的Http客户端模块 来自axios github页面: axios受到Angular提供的$ http服务的启发。 最终axios努力提供一个独立的$ http类服务,以便在Angular之外使用 Angular的(Angular 2+)H ...