【发布时间】:2017-11-14 17:13:41
【问题描述】:
由于我对应用程序进行了很多更改,因此我正在启动此线程以反映它们。我在改变方向时仍然遇到问题。
这是代码
Activity
package com.flash;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
public class QuizActivity extends Activity {
private static final String TAG = "QuizActivity";
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mPrevButton;
private ImageButton mNextButton;
private TextView mQuestionTextView;
private TrueFalse[] mQuestionBank;
private int mCurrentIndex = 0;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle)");
setQuestionBank();
if (getResources().getConfiguration().orientation
== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_quiz);
ProcessPortrait();
} else {
if (getResources().getConfiguration().orientation
== ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setContentView(R.layout.mainland);
ProcessLandscape();
}
}
}
private void ProcessPortrait() {
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
updateQuestion();
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mPrevButton = (ImageButton) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex == 0) {
mCurrentIndex = mQuestionBank.length - 1;
} else {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
}
updateQuestion();
}
});
mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
}
private void ProcessLandscape() {
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
updateQuestion();
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
updateQuestion();
}
});
}
private void setQuestionBank() {
mQuestionBank = new TrueFalse[]{
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_africa, false),
new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia, true)
};
}
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getmQuestion();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismTrueQuestion();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_Toast;
} else {
messageResId = R.string.incorrect_Toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onStart() called");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation
== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setContentView(R.layout.main);
ProcessPortrait();
} else if (getResources().getConfiguration().orientation
== ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setContentView(R.layout.mainland);
ProcessLandscape();
}
}
}
XML
Portrait
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:textSize="10sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="@string/true_button" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="@string/false_button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:contentDescription="@string/prev_button"
android:src="@drawable/arrow_left"/>
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:contentDescription="@string/next_button"
android:src="@drawable/arrow_right"/>
</LinearLayout>
</LinearLayout>
风景
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="24dp"
android:textSize="10sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="@string/true_button" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="@string/false_button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:contentDescription="@string/next_button"
android:gravity="bottom|right"
android:src="@drawable/arrow_right"/>
</LinearLayout>
</FrameLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flash"
android:versionCode="1"
android:versionName="1.0"
>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher" >
<activity android:name=".QuizActivity"
android:label="@string/app_name"
android:configChanges="orientation|
keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
当我使用手机进行调试时,横向可以正常工作,尽管它可能是通过我的手机调整到新的方向而不是应用程序。
我目前在使用模拟器时无法让调试器工作,所以这也是我需要帮助的问题。
当我运行/调试应用程序时,会显示主页,我必须按下模拟器上的最近使用的应用程序按钮,然后双击应用程序以将其向前推进,嗯,又是一个问题。
无论如何,任何帮助将不胜感激
【问题讨论】:
-
您的问题似乎与上一个问题有关。现在请注意:StackOverflow 不是“论坛式”系统;人们不会跟着你从一个问题到另一个问题。如果您的原始问题尚未解决,但您已经取得了进展,请编辑原始问题,例如直接在问题中附加您的进度说明。
-
很抱歉。我已将问题移回原来的问题
-
呃...如果它被删除了,我怎么能看到这个?我也被删除了吗? (但更严重的是,请实际删除问题(应该在编辑旁边),不要只编辑内容)
-
因为据我所知没有办法真正“删除”它。
-
如果你听 onConfigChanges="orientation" 那么你的活动将不会被重新创建
标签: android xml android-layout netbeans