我有一个horizontalscrollview,其中包含许多动态添加的文本视图。当我按下textview时,我希望它的背景保持蓝色,直到我点击另一个textview。在列表视图中,您将使用android:state_activated
,但Android不会让我将其用于textview。我也尝试在onclicklistener中设置背景,但它也没有用。
我怎么能这样做?
活性
public class HorizontaltextscrollerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout menu = (LinearLayout) findViewById(R.id.menu);
for (int i = 0; i < 20; i++) {
TextView t = (TextView) getLayoutInflater().inflate(R.layout.text, null);
t.setId(i);
t.setText("item" + i);
t.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "hi " + v.getId(), Toast.LENGTH_SHORT).show();
v.setBackgroundColor(R.color.background_selected);
}
});
menu.addView(t);
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<LinearLayout
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/text_selector"
android:textSize="24sp"
android:padding="30dp"
android:clickable="true" >
</TextView>
text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/selected" android:state_pressed="true"/>
</selector>
EDIT1
public class HorizontaltextscrollerActivity extends Activity {
TextView[] t = new TextView[20];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout menu = (LinearLayout) findViewById(R.id.menu);
for (int i = 0; i < 20; i++) {
t[i] = (TextView) getLayoutInflater().inflate(R.layout.text, null);
t[i].setId(i);
t[i].setText("item" + i);
t[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "hi " + v.getId(), Toast.LENGTH_SHORT).show();
t[v.getId()].setBackgroundColor(R.color.background_selected);
}
});
menu.addView(t[i]);
}
}
}
答案 0 :(得分:0)
创建一个TextViews数组......
TextView[] t=new TextView[20];
而不是这个..
TextView t = (TextView) getLayoutInflater().inflate(R.layout.text, null);
使用
t[i] = (TextView) getLayoutInflater().inflate(R.layout.text, null);
t[i].setId(i);
//similarly..
t[i].setOnClickListener(new OnClickListener() {
for(int m=0;m<20;m++){
if(m!=i){
t[m].setBackgroundColor(R.color.putdefaultcolor here)
}
}
t[i].setBackgroundColor(R.color.background_selected);
.....
我认为它会正常工作..