我想創建一個菜單選項,用戶可以在其中選擇一個textView可以顯示的顏色。因此,例如,用戶選擇紅色,選擇一個預覽按鈕,它將設置一個textView背景爲紅色。任何建議將不勝感激。Android:用戶選擇textView的顏色背景
public class UserMenu extends Activity implements OnClickListener {
Button preview;
Spinner spinnerColor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_menu);
spinnerColor = (Spinner) findViewById(R.id.spinnerColorMenu);
TextView Title = (TextView)findViewById(R.id.ViewModuleTitle);
preview = (Button)findViewById(R.id.previewButton);
preview.setOnClickListener(this);
}
public void onClick(View v)
{
String color = spinnerColor.getSelectedItem().toString();
Title.setBackgroundResource(R.color.color);
}
}
佈局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/ViewModuleTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/darkBlue"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="@string/addModule"
android:textColor="@color/white"
android:textSize="22dp" />
<TextView
android:id="@+id/lableTextModuleCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enterModuleCode"
android:layout_marginLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
style="@style/textColor"/>
<Spinner
android:id="@+id/spinnerColorMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/colorMenu"/>
<Button
android:id="@+id/previewButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="previewButton"
android:text="@string/addModule" />
</LinearLayout>
知道佈局的樣子會有所幫助 – josephus