在每项活动中隐藏软按钮

时间:2017-07-18 20:06:40

标签: java android android-immersive

我的应用程序中有3个活动,主要活动和其他2个活动。在主要内部,我在onCreate方法中使用此代码使应用程序全屏运行:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    this.findViewById(R.id.main_screen).setOnTouchListener(this);

    width = Util.getInstance().getScreenWidth(this);
    height = Util.getInstance().getScreenHeight(this);

    String imagePath = PreferenceManager.getDefaultSharedPreferences(this).getString("IMAGE_PATH", "NULL");
    if (!"NULL".equals(imagePath) && new File(imagePath).exists()){
        Util.IMAGE_PATH = imagePath;
        setImage();
    }
}

我在其他2个活动中使用了相同的代码,如下所示,但它们似乎没有任何效果。它仍然不会像主要人那样全屏显示。以下是其他两项活动:

public class Pass extends Activity implements OnClickListener, OnKeyListener, OnTouchListener{


EditText etPassword;
    public static StringBuilder pass;
    public static String replace;
    static boolean keyDel = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pass);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        etPassword = (EditText) findViewById(R.id.password);

        final StringBuilder stringBuilderHiddenPassword = new StringBuilder();
        final StringBuilder mPass = new StringBuilder();
        pass = new StringBuilder();
        etPassword.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_DEL){
                    keyDel = true;
                }else{
                    keyDel = false;
                }
                return false;
            }
        });

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!keyDel){
                    stringBuilderHiddenPassword.append("*");
                    etPassword.removeTextChangedListener(this);
                    etPassword.setText(stringBuilderHiddenPassword);
                    etPassword.addTextChangedListener(this);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                pass.append(s);
                replace = pass.toString().replace("*", "");
            }
        });

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        this.findViewById(R.id.pass_screen).setBackgroundColor(Color.argb(100, 0, 0, 0));

        this.findViewById(R.id.pass_cancel).setOnClickListener(this);
        this.findViewById(R.id.pass_ok).setOnClickListener(this);
        this.findViewById(R.id.password).setOnKeyListener(this);
        this.findViewById(R.id.pass_screen).setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.pass_screen) {
            this.finish();
            return true;
        }

        else return false;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case(R.id.pass_cancel): this.finish(); break;
        case(R.id.pass_ok): checkPass(); break;
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != MotionEvent.ACTION_UP)
            return false;

        switch(keyCode){
        case(KeyEvent.KEYCODE_ENTER):
        case(KeyEvent.KEYCODE_DPAD_CENTER): checkPass(); break;
        default: return false;
        }



return true;
}

private void checkPass(){
    if (replace.equals(Util.PASS)){
        showMenu(this);
    }
    else {
        ((EditText)this.findViewById(R.id.password)).setText("");
        Util.getInstance().error("Wrong Password!", this);
    }
}

private void showMenu(final Activity act){
    Thread t = new Thread(){
        public void run(){
            act.finish();
            act.startActivity(new Intent(act, Menu.class));
        }
    };
    t.start();
}
}

另一个:

public class Menu extends Activity implements OnItemClickListener {
    private GridView thumbs;
    private ImageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        adapter = new ImageAdapter(getApplicationContext());

        thumbs = (GridView)this.findViewById(R.id.thumbs);
        thumbs.setAdapter(adapter);
        thumbs.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        ((Layout)v).setFocus(true);
        setImage(adapter.getItem(position));
    }

    private void setImage(String pic){
        Util.IMAGE_PATH = pic;
        Util.SET_IMAGE_FLAG = true;

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putString("IMAGE_PATH", Util.IMAGE_PATH).commit();

        this.finish();
    }
}

由于不熟悉沉浸式模式,我希望你们中的一些人可以提供答案。

2 个答案:

答案 0 :(得分:1)

在您的Manifest.xml文件中添加活动标记

中的属性
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

答案 1 :(得分:0)

而不是在所有活动中使用setSystemUiVisibility,您可以从Activity创建简单的类扩展并将其用作父级,例如

父:

public class BaseActivity extends Activity {
    private GridView thumbs;
    private ImageAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    }
}

子:

public class Pass extends BaseActivity implements OnClickListener, OnKeyListener, OnTouchListener{

...

}

更新1:测试此解决方案

创建自定义样式

<style name="Theme.myFullScreen" parent="android:Theme">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

在清单上设置:

android:theme="@style/Theme.myFullScreen"