防止EditText的扩展

时间:2011-12-28 10:40:26

标签: android android-layout android-edittext android-softkeyboard

我有以下布局文件

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.company" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC"
        android:orientation="horizontal" 
        android:id="@+id/main">

        <com.company.DrawView
            android:id="@+id/draw_view" 
            android:layout_width="900dp"
            android:layout_height="500dp"
            android:layout_alignParentLeft="true"
            android:background="#CCCC00" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/draw_view"
            android:orientation="horizontal">
        <Button
            android:id="@+id/btn1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="10dp"
            android:clickable="true"
            android:onClick="buttonClicked"
            android:text="button" />

        <EditText 
            android:id="@+id/text1"
            android:textSize="5dp"
            android:inputType="text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:maxWidth="5dp"
            android:visibility="invisible"
            android:gravity="bottom"
            />
        </LinearLayout>
    </RelativeLayout>

每当我将焦点设置在EditText上时,它就会扩展并填满屏幕的上半部分,软键盘会填满屏幕的下半部分。我应该怎么做才能保留EditText的大小并显示软键盘?

目的是捕获用户的击键(在软键盘上)并在应用程序的另一部分上使用它。

编辑1: 我正在使用sdk-15版本,如果有帮助我就在macbook上。添加清单文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ChalkrText"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustNothing" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

3 个答案:

答案 0 :(得分:0)

您在manifest.xml中的

//在您的活动中编写以下代码

android:windowSoftInputMode="adjustNothing" 

答案 1 :(得分:0)

这是设备横向方向EditText的标准行为。你无法改变它。

答案 2 :(得分:0)

我通过单击按钮显示软键盘并覆盖活动中的dispatchKeyEvent以捕获击键来找到解决方法。我不需要EditText,也不需要显示软键盘。

这是为了显示软键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

这是dispatchKeyEvent:

public boolean dispatchKeyEvent(KeyEvent event) {
/**
 * ACTION_MULTIPLE is for keys with special characters like &copy;
 * British Pound sign etc. These keys do not generate the ACTION_DOWN
 * or ACTION_UP events.
 * 
 * Only the ACTION_DOWN is handled. ACTION_UP is ignored to avoid
 * duplication of the letters that the user wants to type.
 */
if (event.getAction() == KeyEvent.ACTION_DOWN ||
    event.getAction() == KeyEvent.ACTION_MULTIPLE) {

    DrawView dview = (DrawView) findViewById(R.id.draw_view);
        Log.v(LOG_TAG, "c = " + event.getKeyCode() + 
                    ", l = " + event.getDisplayLabel() + 
                    ", u = " + event.getUnicodeChar() +
                    ", uc = " + (char)event.getUnicodeChar() + 
                    ", chars = " + event.getCharacters()
            );
        dview.addText(event);
}
return true;
}

这是处理相关事件的addText方法:

public void addText(KeyEvent event) {
    int unicode = event.getUnicodeChar();
    int keycode = event.getKeyCode();
    String characters = event.getCharacters();

    if (unicode == 0) {
        switch (keycode) {
        case 0:
            if (characters != null) {
                text += characters;
            }
            break;
        case KeyEvent.KEYCODE_DEL:
            text = text.substring(0, text.length()-1);
            break;
        case KeyEvent.KEYCODE_SHIFT_LEFT:
        case KeyEvent.KEYCODE_SHIFT_RIGHT:
            // ignore, nothing to do
            break;
        }
    }
    else {
        text = text + (char)unicode;
    }

    invalidate();
}

这些帖子让我朝着正确的方向前进: How to Capture soft keyboard input in a View? Can I use the soft keyboard without an EditText?