我想创建一个包含2个文本视图和2个编辑文本的应用。这里的逻辑是,当输入<= 30时,textview的背景颜色变为蓝色。如果输入<= 60,则变为红色。如果inut超过60,则变为绿色。
因此,根据两个编辑文本中一次给出的输入,文本视图的背景颜色应根据逻辑进行更改。(编辑文本中输入的输入和输出显示为文本视图的背景颜色)。 / p>
这是我的代码。
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
TextView txblue,txred;
EditText inp1,inp2;
Button ok;
int numbr = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txblue = (TextView) this.findViewById(R.id.tblue);
this.txred = (TextView) this.findViewById(R.id.tred);
this.inp1 = (EditText) this.findViewById(R.id.editText1);
this.inp2 = (EditText) this.findViewById(R.id.editText2);
this.ok = (Button) this.findViewById(R.id.button1);
inp1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(getBaseContext(), "Only 10 numbers",
Toast.LENGTH_LONG);
msg.show();
}
});
inp2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast msg = Toast.makeText(getBaseContext(), "Only 10 numbers",
Toast.LENGTH_LONG);
msg.show();
}
});
this.ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (numbr <= 30)
{
txblue.setBackgroundColor(Color.BLUE);
}
else if (numbr <= 60)
{
txblue.setBackgroundColor(Color.RED);
}
else
{
txblue.setBackgroundColor(Color.GREEN);
}
return;
}
});
}
public void onClick(View v)
{
if (numbr <= 30)
{
txblue.setBackgroundColor(Color.BLUE);
}
else if (numbr <= 60)
{
txblue.setBackgroundColor(Color.RED);
}
else
{
txblue.setBackgroundColor(Color.GREEN);
}
return;
}
}
答案 0 :(得分:1)
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
}
}
});
希望这会对你有所帮助
答案 1 :(得分:1)
请试试这个,我希望它能帮到你。
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
}
});