【问题标题】:Changing Background color for TextView on click in Android using Java使用 Java 在 Android 中单击时更改 TextView 的背景颜色
【发布时间】:2019-10-24 09:32:00
【问题描述】:

在我的 Android 应用中,我的类别标题如图所示。当我点击下面的标题时,内容将根据类别标题而改变。同时标题,背景颜色应该改变,但其他标题背景颜色应该保持不变。如何在 Android Studio 中使用 Java 做到这一点?

【问题讨论】:

  • 添加更多代码理解
  • 我的标题是动态的

标签: java android


【解决方案1】:

您可以像这样更改 TextView 的背景颜色:

yourTextViewTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    yourContentTextView.setBackgroundColor(getResources()
                            .getColor(R.color.yourColor));
                }
            });

【讨论】:

  • 应用程序崩溃
  • 您是否将 yourTextViewTitle、yourContentTextView 和 yourColor 替换为您的字段?
  • 您可以编辑您的问题并发布一些到目前为止您已经尝试过的代码,以便我们了解您的问题。
【解决方案2】:

对于每个视图,都有一个 setBackgroundColor 方法

yourView.setBackgroundColor(Color.parseColor("#ffffff"));

【讨论】:

  • 我的标题是动态的
【解决方案3】:

尝试如下:

...

title1, title2, title3, selectedTitle;

...

title1.setOnClickListener(v -> {
    updateTitleBackground(title1);
});

title2.setOnClickListener(v -> {
    updateTitleBackground(title2);
});

title3.setOnClickListener(v -> {
    updateTitleBackground(title3);
});

...

private void updateTitleBackground(title) {
    if(selectedTitle != null) {
        selectedTitle.setBackgroundColor(Color.WHITE);
    }

    selectedTitle = title;
    title.setBackgroundColor(Color.RED);
}

...

【讨论】:

  • 我的标题是动态的...检查我的答案
  • 添加一些你目前已经完成的实现。否则很难提供解决方案。我给了你一个想法,无论它是动态的还是静态的
【解决方案4】:

以下是完成目标的说明和代码 -

String titleContent1 = "Content 1", titleContent2 = "Content 2", titleContent3 = "Content 3";

textView1.setOnClickListener(v -> {
    changeBackgroundColorAndTitle(titleContent1, getResources().getColor(R.color.red));
});


textView2.setOnClickListener(v -> {
    changeBackgroundColorAndTitle(titleContent2, getResources().getColor(R.color.green));
});


textView3.setOnClickListener(v -> {
    changeBackgroundColorAndTitle(titleContent3, getResources().getColor(R.color.white));
});

private void changeBackgroundColorAndTitle(String titleContent, int color) {
    selectedTitle = title;
    contentTextView.setBackgroundColor(color);
}

【讨论】:

    【解决方案5】:

    这样试试

    // initialize your views inside your onCreate()
    title1 = findViewById("yourtextviewid");
    title2 = findViewById("yourtextviewid");
    title3 = findViewById("yourtextviewid");
    
    title1.setOnClickListener(this);
    title2.setOnClickListener(this);
    title3.setOnClickListener(this);
    

    在您的onClick 方法中

    @Override
    public void onClick(View view) {
       if(view ==  title1)
          // here i assumed yourContentView and contenTextview you should use yours.
          yourContentView.setBackgroundColor(Color.RED); 
          contentTextView.setText("Title1");
       }else if(view ==  title2){
          yourContentView.setBackgroundColor(Color.GREEN);
          contentTextView.setText("Title2");
       }else if(view ==  title3){
          yourContentView.setBackgroundColor(Color.BLACK);
          contentTextView.setText("Title3");
       }
    ``
    
    

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 2014-06-12
      • 2011-06-05
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多