我正在尝试为我的线性布局(称为容器)添加2行动画。单击主题按钮(mathButton或bioButton)时会调用这两行。
我要添加的第一行(带字符串&#34的TextView;设置难度:")。接下来是一排三个ToggleButtons,允许用户选择难度(" Easy"," Medium"," Hard")。
当我选择主题时,TextView对象难题标题会显示动画。但是,按钮不是。我不确定为什么会这样。这里的任何帮助都会很棒!
活动代码:
public class LaunchScreen extends Activity {
private TextView gameTitle;
private Button startGame;
private ToggleButton mathButton;
private ToggleButton bioButton;
private CharSequence difficultyLevel;
private CharSequence topic;
private Boolean lessonModeOn;
private Boolean difficultyAppeared;
private Boolean lessonModeAppeared;
private TextView difficultyTitle;
private ViewGroup mContainerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_screen);
mContainerView = (ViewGroup) findViewById(R.id.gameSettingsContainer);
//booleans to keep track of added difficulty and lesson mode
difficultyAppeared = false;
lessonModeAppeared = false;
gameTitle = (TextView) findViewById(R.id.gameTitle);
gameTitle.setText("BrainTeaz!");
startGame = (Button) findViewById(R.id.startGame);
startGame.setText("Start");
Toast.makeText(LaunchScreen.this, "Welcome! Select a topic.", Toast.LENGTH_SHORT).show();
topic = null;
mathButton = (ToggleButton) findViewById(R.id.mathButton);
mathButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
topic = mathButton.getText();
bioButton.setChecked(false);
}
else {
if(!bioButton.isChecked()) {
topic = null;
}
}
if (!difficultyAppeared) {
animateDifficulty();
}
}
});
bioButton = (ToggleButton) findViewById(R.id.bioButton);
bioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
topic = bioButton.getText();
mathButton.setChecked(false);
}
else {
if(!mathButton.isChecked())
{
topic = null;
}
}
if (!difficultyAppeared) {
animateDifficulty();
}
}
});
}
public void animateDifficulty() {
Toast.makeText(LaunchScreen.this, "Choose a difficulty.", Toast.LENGTH_SHORT).show();
ViewGroup newView1 = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.launch_screen_difficulty_settings_title, mContainerView, false);
ViewGroup newView2 = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.launch_screen_difficulty_settings, mContainerView, false);
difficultyLevel = null;
final ToggleButton easyDifficulty = (ToggleButton) newView2.findViewById(R.id.easyDifficulty);
final ToggleButton medDifficulty = (ToggleButton) newView2.findViewById(R.id.medDifficulty);
final ToggleButton hardDifficulty = (ToggleButton) newView2.findViewById(R.id.hardDifficulty);
easyDifficulty.setChecked(false);
medDifficulty.setChecked(false);
hardDifficulty.setChecked(false);
easyDifficulty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
difficultyLevel = easyDifficulty.getText();
//guarantee no other button is checked
medDifficulty.setChecked(false);
hardDifficulty.setChecked(false);
} else {
difficultyLevel = null;
// The toggle is disabled
// The toggle is disabled, enable easy as default
if (!hardDifficulty.isChecked() && !medDifficulty.isChecked()) {
difficultyLevel = null;
}
}
if (!lessonModeAppeared) {
animateLessonMode();
}
}
});
medDifficulty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
difficultyLevel = medDifficulty.getText();
//guarantee no other button is checked
easyDifficulty.setChecked(false);
hardDifficulty.setChecked(false);
} else {
difficultyLevel = null;
// The toggle is disabled, enable easy as default
//if both aren't checked then
if (!easyDifficulty.isChecked() && !hardDifficulty.isChecked()) {
difficultyLevel = null;
}
}
if (!lessonModeAppeared) {
animateLessonMode();
}
}
});
hardDifficulty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
difficultyLevel = hardDifficulty.getText();
Toast.makeText(getApplicationContext(), "Hard difficulty selected.", Toast.LENGTH_SHORT).show();
//guarantee no other button is checked
easyDifficulty.setChecked(false);
medDifficulty.setChecked(false);
} else {
if (!easyDifficulty.isChecked() && !medDifficulty.isChecked()) {
difficultyLevel = null;
}
}
if (!lessonModeAppeared) {
animateLessonMode();
}
}
});
mContainerView.addView(newView1, 4);
Log.d("vikram", "you should see difficulty title");
mContainerView.addView(newView2, 5);
Log.d("vikram","you should see buttons");
difficultyAppeared = true;
}
LaunchScreen的布局:
<?xml version="1.0" encoding="utf-8"?>
<!--
Game settings will be programmatically added to this parent Linear Layout
-->
<LinearLayout
android:id="@+id/gameSettingsContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.vb1115.multchoicequestion.LaunchScreen"
android:orientation="vertical"
android:animateLayoutChanges="true">
<TextView android:id="@+id/gameTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Title"
android:textSize="40dp"
android:layout_gravity="center_horizontal"/>
<Button android:id="@+id/startGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_gravity="center_horizontal"
android:onClick="startGame"/>
<!--
Set Topic - Once this is clicked, new 2 rows of game settings
will be animated
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Topic:"
android:textSize="20dp"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<ToggleButton android:id="@+id/mathButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Math"
android:textOn="Math"
android:text="Math"/>
<ToggleButton android:id="@+id/bioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Biology"
android:textOn="Biology"
android:text="Biology"/>
</LinearLayout>
</LinearLayout>
第一行的布局(launch_screen_difficulty_settings_title.xml)(newView1):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/difficultyTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Difficulty:"
android:textSize="20dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
第二行的布局(launch_screen_difficulty_settings)(newView2):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ToggleButton android:id="@+id/easyDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Easy"
android:textOn="Easy"
android:text="Easy"/>
<ToggleButton android:id="@+id/medDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Med"
android:textOn="Med"
android:text="Med"/>
<ToggleButton android:id="@+id/hardDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Hard"
android:textOn="Hard"
android:text="Hard"/>
</LinearLayout>
答案 0 :(得分:0)
以下是我为解决问题所做的更改(两行):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
**android:layout_height="wrap_content"**
再次感谢Beyka!