我想知道 如何在按下另一个视图时更改某些视图的属性?
例如,当按下另一个按钮时,textview会改变它的颜色吗?
答案 0 :(得分:1)
为已按下的View
注册OnTouchListener
,并使用onTouch
方法更改TextView
的属性。
答案 1 :(得分:0)
这是给你的演示 在java中
name = (EditText) findViewById(R.id.editText1);
pwd = (EditText) findViewById(R.id.editText2);
sp=getSharedPreferences("userdata",MODE_PRIVATE);
speditor=sp.edit();
log = (Button) findViewById(R.id.button1);
log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(name.getText().toString().isEmpty())
{
name.setBackgroundColor(Color.RED);
Toast.makeText(getBaseContext(), "Invalid Username !!! :", Toast.LENGTH_SHORT).show();
}
if((name.isFocusable()==true)||(pwd.isFocusable()==true))
{
name.setBackgroundColor(Color.WHITE);
pwd.setBackgroundColor(Color.WHITE);
}
if(pwd.getText().toString().isEmpty())
{
pwd.setBackgroundColor(Color.RED);
Toast.makeText(getBaseContext(), "Invalid Password !!! :", Toast.LENGTH_SHORT).show();
}
String n,p;
n=sp.getString("NAME","");
p=sp.getString("PASWRD","");
if((name.getText().toString().equals(n))&&(pwd.getText().toString().equals(p))){
Toast.makeText(getBaseContext(), "Welcome "+name.getText().toString(),Toast.LENGTH_LONG).show();
Intent nd=new Intent(getBaseContext(),DiaryActivity.class);
startActivity(nd);
}
}
});
reg = (Button) findViewById(R.id.button2);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(name.getText().toString().isEmpty())
{
name.setBackgroundColor(Color.RED);
Toast.makeText(getBaseContext(), "Invalid Username !!! :", Toast.LENGTH_SHORT).show();
}
if((name.isFocusable()==true)||(pwd.isFocusable()==true))
{
name.setBackgroundColor(Color.WHITE);
pwd.setBackgroundColor(Color.WHITE);
}
if(pwd.getText().toString().isEmpty())
{
pwd.setBackgroundColor(Color.RED);
Toast.makeText(getBaseContext(), "Invalid Password !!! :", Toast.LENGTH_SHORT).show();
}
if((!pwd.getText().toString().isEmpty())&&(!name.getText().toString().isEmpty())){
speditor.putString("NAME",name.getText().toString());
speditor.putString("PASWRD",pwd.getText().toString());
speditor.commit();
Toast.makeText(getBaseContext(),"Hello "+name.getText().toString()+" You are Registered",Toast.LENGTH_LONG).show();
}
}
});
答案 2 :(得分:0)
这只是样本:
TextView txt1 = (TextView) findViewById(R.id.text1);
TextView txt2 = (TextView) findViewById(R.id.text2);
和一个按钮
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View back_button)
{
txt1.setBackgroundColor(Color.BLACK);
txt2.setBackgroundColor(Color.BLACK);
}});
此处按钮单击时,textview的背景颜色变为黑色。尝试使用其他属性