Android:退出全屏并返回主要活动

时间:2017-11-16 22:13:58

标签: android android-activity fullscreen

主要活动包含微调器和按钮。 MainActivity.java代码如下。

public class MainActivity extends AppCompatActivity implements LoadNew.VCallback {

    public Spinner dropdown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dropdown = (Spinner) findViewById(R.id.spinner1);

        //Create a list of items for the spinner.
        String[] items = new String[]{"select topic", "topic1", "topic2"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);
        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // TODO Auto-generated method stub
                String sp1 = String.valueOf(dropdown.getSelectedItem());
                if (sp1.contentEquals("select topic")) {
                    loadFun("");
                }
                if (sp1.contentEquals("topic1")) {
                    loadFun("topic1");
                }
                if (sp1.contentEquals("topic2")) {
                    loadFun("topic2");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView){
                // TODO Auto-generated method stub
            }
        });

        Button x = (Button) findViewById(R.id.full_screen);
        x.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadFun("topic2", 0);
            }
        });
    }

    public void loadFun(String topicName, int i) {
        LoadNew st = new LoadNew(this);
        st.display(topicName, i);
    }
}

主要活动的xml布局如下(activity_main.xml)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:drawable/btn_dropdown"
                android:spinnerMode="dropdown" />
        </LinearLayout>
        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal" >
             <Button
                android:id="@+id/full_screen"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Full Screen"
                style="?android:attr/borderlessButtonStyle"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/full_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

还有另一个java类LoadNew.java如下。

public class LoadNew {

    public interface VCallback {
        //To access methods of MainActivity class
    }

    public Activity activity;

    public LoadNew(Activity _activity) {
        this.activity = _activity;
        VCallback callerActivity = (VCallback) activity;

    }

    //Display PDF
    public void display(String topicName, int i) {
        LinearLayout fullScreen = (LinearLayout) this.activity.findViewById(R.id.full_view);
        fullScreen.removeAllViews();//Clear field
        LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.activity_load, null);
        if(i ==0) {
         this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            this.activity.setContentView(view);
        }
        TextView tv= (TextView) this.activity.findViewById(R.id.tv1);
        tv.setText(topicName);
        tv.setTextSize(100);
    }
}

activity_load.xml文件如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv1"
    android:layout_below="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</RelativeLayout>

在微调器中选择某个项目时,相应的文本将显示在textview中。按下按钮后,textview将以全屏模式显示。 现在我想返回上一个视图(通过按下后退按钮或屏幕上的单个选项卡),即单击按钮之前的视图。我怎样才能做到这一点?提前感谢您的耐心和帮助。

1 个答案:

答案 0 :(得分:0)

最简单的方法是在一个活动上安装微调器和非全屏TextView,并在按钮点击时使用全屏模式打开第二个活动。