Android Api Demos登顶之路(二十三)SoftInputModes

这个demo演示了当系统的软键盘打开时,window布局的调整方式 可以通过设定WindowManager.LayoutParams参数来实现不同的调整模式
activity_main.xml

<LinearLayout 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"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:paddingTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:text="Resize mode:"/>
        <Spinner 
            android:id="@+id/resizeMode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="This is the part of application's UI that can resize to adjust for the IME"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:background="#ff0000" />
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="#00ff00"
        android:text="Text editor.Tap to show the IME,which will cause to this window to resize as requested"/>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {
    private Spinner mResizeMode;
    private CharSequence[] mModeLabels = new CharSequence[] { "Unspecified",
            "Resize", "Pan", "Nothing" };
    private int[] mResizeModes = new int[] {
            // 未指定键盘的显示模式,系统根据内容自行从resize和pan两种模式中选择一种
            // 或是继续使用上次的显示模式
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED,
            // 整个Layout重新编排,重新分配多余空间
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE,
            // 把整个Layout顶上去露出获得焦点的EditText,确保输入焦点可见
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN,
            // 不调整(输入法完全直接覆盖住)
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mResizeMode = (Spinner) findViewById(R.id.resizeMode);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                this, android.R.layout.simple_spinner_item, mModeLabels);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mResizeMode.setAdapter(adapter);
        mResizeMode.setSelection(0);
        mResizeMode.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                getWindow().setSoftInputMode(mResizeModes[position]);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                getWindow().setSoftInputMode(mResizeModes[0]);
            }
        });
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值