gpt4 book ai didi

java - 单击按钮时,edittext 中的背景颜色和字体系列应更改

转载 作者:行者123 更新时间:2023-12-01 19:14:05 27 4
gpt4 key购买 nike

我正在尝试创建一个包含编辑文本的 Activity ,每当单击按钮时,应更改其背景颜色和字体。为了在编辑文本上执行这些操作,我创建了两个 int 值的 ArrayList,并将所有颜色的 id 和字体的 id 保存在这个 ArrayList 中。每当单击按钮时,背景颜色都没有改变,但颜色的十六进制代码打印在编辑文本中,并且这些颜色代码在每种颜色代码前面带有额外的 ff 值,如 #ff1EFA9D,并且在字体方法中我收到错误说:-

无法启动 Activity ComponentInfo{com.nanb.alpha/com.nanb.alpha.postcreater}: java.lang.ArrayIndexOutOfBoundsException: length=4;索引=-1

我尝试通过互联网找到解决方案,但没有得到任何满足该错误的解决方案。

下面给出了更改编辑文本的背景颜色和字体的代码

  private EditText editText;
private ImageButton color,font;
ArrayList<Integer> bgcolorlist = new ArrayList<Integer>(10);
ArrayList<Integer> fontstyle = new ArrayList<Integer>(6);
int fontstylename=R.font.abril_fatface,bgcolor=R.color.white,fontpostion = fontstyle.indexOf(fontstylename);
int postion = bgcolorlist.indexOf(bgcolor);


private void Initialization() {
cancel = view.findViewById(R.id.backbutton);
createnextbutton = view.findViewById(R.id.nextbutton);
editText = view.findViewById(R.id.textpost);
color = view.findViewById(R.id.bgcolorbutton);
font = view.findViewById(R.id.fontstyle);
}

//codes are for the background color changing
private void backgroundcolormethod() {

bgcolorlist.add(R.color.white);
bgcolorlist.add(R.color.darkGreen);
bgcolorlist.add(R.color.colorPrimaryDark);
bgcolorlist.add(R.color.colorAccent);
bgcolorlist.add(R.color.colorPrimary);
bgcolorlist.add(R.color.orange);
bgcolorlist.add(R.color.edittextbg5);
bgcolorlist.add(R.color.edittextbg4);
bgcolorlist.add(R.color.edittextbg3);
bgcolorlist.add(R.color.edittextbg2);
bgcolorlist.add(R.color.edittextbg1);

editText.setBackgroundColor(bgcolor);

color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(postion == 10){
//Toast.makeText(view.getContext(),"last element",Toast.LENGTH_SHORT).show();
editText.setText(bgcolorlist.get(postion));
postion = 0;
}else{
int newpostion = postion + 1;
editText.setText(bgcolorlist.get(newpostion));
postion = newpostion;
}
}
});

}

//codes for changing the fonts
private void fontstylemethod() {
fontstyle.add(R.font.abril_fatface);
fontstyle.add(R.font.cedarville_cursive);
fontstyle.add(R.font.alfa_slab_one);
fontstyle.add(R.font.annie_use_your_telescope);
fontstyle.add(R.font.aclonica);
fontstyle.add(R.font.aguafina_script);
fontstyle.add(R.font.arizonia);

editText.setTypeface(Typeface.defaultFromStyle(fontpostion));

font.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fontpostion == 6){
Toast.makeText(view.getContext(),"last element",Toast.LENGTH_SHORT).show();
editText.setText(fontstyle.get(postion));
fontpostion= 0;
}else{
int newpostion = fontpostion + 1;
editText.setText(fontstyle.get(newpostion));
fontpostion = newpostion;
}
}
});
}

帮助我解决这些错误。

最佳答案

尝试将代码更改为:

  ArrayList<Integer> bgcolorlist = new ArrayList<Integer>(11);
ArrayList<Integer> fontstyle = new ArrayList<Integer>(7);
int fontpostion = 0;
int postion = 0;


private void Initialization() {
cancel = view.findViewById(R.id.backbutton);
createnextbutton = view.findViewById(R.id.nextbutton);
editText = view.findViewById(R.id.textpost);
color = view.findViewById(R.id.bgcolorbutton);
font = view.findViewById(R.id.fontstyle);
}

//codes are for the background color changing
private void backgroundcolormethod() {

bgcolorlist.add(R.color.white);
bgcolorlist.add(R.color.darkGreen);
bgcolorlist.add(R.color.colorPrimaryDark);
bgcolorlist.add(R.color.colorAccent);
bgcolorlist.add(R.color.colorPrimary);
bgcolorlist.add(R.color.orange);
bgcolorlist.add(R.color.edittextbg5);
bgcolorlist.add(R.color.edittextbg4);
bgcolorlist.add(R.color.edittextbg3);
bgcolorlist.add(R.color.edittextbg2);
bgcolorlist.add(R.color.edittextbg1);

editText.setBackgroundColor(bgcolorlist.get(0));

color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(postion == 10){
//Toast.makeText(view.getContext(),"last element",Toast.LENGTH_SHORT).show();
editText.setText(bgcolorlist.get(postion));
postion = 0;
}else{
postion++;
editText.setText(bgcolorlist.get(postion));
}
}
});

}

//codes for changing the fonts
private void fontstylemethod() {
fontstyle.add(R.font.abril_fatface);
fontstyle.add(R.font.cedarville_cursive);
fontstyle.add(R.font.alfa_slab_one);
fontstyle.add(R.font.annie_use_your_telescope);
fontstyle.add(R.font.aclonica);
fontstyle.add(R.font.aguafina_script);
fontstyle.add(R.font.arizonia);

editText.setTypeface(Typeface.defaultFromStyle(fontstyle.get(0)));

font.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fontpostion == 6){
Toast.makeText(view.getContext(),"last element",Toast.LENGTH_SHORT).show();
editText.setText(fontstyle.get(postion));
fontpostion= 0;
}else{
fontpostion++;
editText.setText(fontstyle.get(fontpostion));
}
}
});
}

关于java - 单击按钮时,edittext 中的背景颜色和字体系列应更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59438586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com