Android 如何根据用户输入更改颜色?
我想创建一个应用程序,其中有2个文本视图和2个编辑文本。这里的逻辑是,当输入是Android 如何根据用户输入更改颜色?,android,colors,textview,Android,Colors,Textview,我想创建一个应用程序,其中有2个文本视图和2个编辑文本。这里的逻辑是,当输入是editText.setInputType(InputType.TYPE\u NUMBER\u FLAG\u SIGNED)时; editText.addTextChangedListener(新的TextWatcher() { @凌驾 public void onTextChanged(字符序列、int start、int before、int count) { //TODO自动生成的方法存根 } @凌驾 更改前文本
editText.setInputType(InputType.TYPE\u NUMBER\u FLAG\u SIGNED)时;
editText.addTextChangedListener(新的TextWatcher()
{
@凌驾
public void onTextChanged(字符序列、int start、int before、int count)
{
//TODO自动生成的方法存根
}
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after)
{
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑)
{
//TODO自动生成的方法存根
int i=Integer.parseInt(s.toString());
如果(我请试试这个,我希望它能帮助你
final EditText inputtext = (EditText) findViewById(R.id.editText1);
inputtext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
if (Integer.parseInt(inputtext.getText().toString()) <= 30) {
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
t1.setTextColor(Color.GREEN);
t2.setTextColor(Color.GREEN);
} else if (Integer.parseInt(inputtext.getText().toString()) <= 60) {
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
t1.setTextColor(Color.BLUE);
t2.setTextColor(Color.BLUE);
}else if (Integer.parseInt(inputtext.getText().toString()) > 60) {
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
t1.setTextColor(Color.RED);
t2.setTextColor(Color.RED);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
final EditText inputtext=(EditText)findViewById(R.id.editText1);
inputtext.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs,int start,int before,
整数计数){
if(Integer.parseInt(inputtext.getText().toString())那么你的问题在哪里呢?好的,你的问题是什么…谢谢你的回答。它有点有效。但现在的问题是,当我输入50时,它变为蓝色。实际上根据逻辑,它是错误的。你的输入是文本还是数字?数字。我在编辑文本1中输入50,然后单击“确定”按钮。textview的背景色变为蓝色。谢谢你很多。它奏效了。现在我明白我错在哪里了。非常感谢你的回答。它也奏效了。再次感谢。
editText.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
editText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
int i=Integer.parseInt(s.toString());
if (i <= 30)
{
//blue color bkg
}
else if (i <= 60)
{
//red color bkg
}
else if (i > 60)
{
//green color bkg
}
}
});
final EditText inputtext = (EditText) findViewById(R.id.editText1);
inputtext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
if (Integer.parseInt(inputtext.getText().toString()) <= 30) {
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
t1.setTextColor(Color.GREEN);
t2.setTextColor(Color.GREEN);
} else if (Integer.parseInt(inputtext.getText().toString()) <= 60) {
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
t1.setTextColor(Color.BLUE);
t2.setTextColor(Color.BLUE);
}else if (Integer.parseInt(inputtext.getText().toString()) > 60) {
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
t1.setTextColor(Color.RED);
t2.setTextColor(Color.RED);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});