0

目前我正在开发一个基于自定义键盘的应用程序。基本上,我有一个带有和编辑文本的布局和一个带有键盘布局的辅助 xml。该应用程序应该启动,隐藏默认键盘并显示自定义键盘。

几天前我完成了这个应用程序,我正在用 2.1 2.2 和 2.3 的 AVD 测试它,它们都像魅力一样工作!

之后我决定在实际设备上尝试,但自定义键盘不起作用,所以我调试了它。为键设置 onclick 侦听器时会出现问题,它们都抛出 nullPointerException。

真正让我感到困惑的是,它在 AVD 上完美运行,也为 AVD 调试了它,根本没有 nullPointerException。

这是正常的吗?

代码如下:

public class Main extends Activity implements OnTouchListener, OnClickListener,
    OnFocusChangeListener {
private EditText mEt;
private Button mBSpace, mBack, mBorrar;
private RelativeLayout mLayout, mKLayout;
private boolean isEdit = true; 
private int w, mWindowWidth;
private String cL[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "Ñ", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
        "X", "Y", "Z", ".", "?",  "!"}; 
private Button mB[] = new Button[40];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.main);
        setKeys();  //get ids from xml and setOnClickListeners for every button.
        mEt = (EditText) findViewById(R.id.xEt);
        mEt.setOnTouchListener(this);
        mEt.setOnFocusChangeListener(this);
        mEt.setOnClickListener(this);
        mLayout = (RelativeLayout) findViewById(R.id.xK1);
        mKLayout = (RelativeLayout) findViewById(R.id.xKeyBoard);
        hideDefaultKeyboard(); //abrir teclado al prender app
        enableKeyboard();
        changeCapitalLetters();
        changeCapitalTags();

    } catch (Exception e) {
        Log.w(getClass().getName(), e.toString());
    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (v == mEt) {
        hideDefaultKeyboard();
        enableKeyboard();
    }
    return true;
}

@Override
public void onClick(View v) {

    if (v != mBack && v != mBorrar) {

        addText(v);

    } else if (v != mBorrar  && v == mBack) {
        isBack(v);
    } else if (v != mBack && v == mBorrar) {
        isBorrar(v);
    }
}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (v == mEt && hasFocus == true) {

        isEdit = true;

    } 

}

private void addText(View v) {
    if (isEdit == true) {
        String b = "";
        b = (String) v.getTag();
        if (b != null) {
            // adding text in Edittext
            mEt.append(b);

        }
    }

}

private void isBack(View v) {
    if (isEdit == true) {
        CharSequence cc = mEt.getText();
        if (cc != null && cc.length() > 0) {
            {
                mEt.setText("");
                mEt.append(cc.subSequence(0, cc.length() - 1));
            }

        }
    }

}
private void isBorrar(View v) {
    if (isEdit == true) {
        CharSequence cc = mEt.getText();
        if (cc != null && cc.length() > 0) {
            {
                mEt.setText("");
            }

        }
    }

}
private void changeCapitalLetters() {
    for (int i = 0; i < cL.length; i++)
        mB[i].setText(cL[i]);
}

private void changeCapitalTags() {
    for (int i = 0; i < cL.length; i++)
        mB[i].setTag(cL[i]);
}

// enabling customized keyboard
private void enableKeyboard() {
    mLayout.setVisibility(RelativeLayout.VISIBLE);
    mKLayout.setVisibility(RelativeLayout.VISIBLE);

}

// Disable customized keyboard
private void disableKeyboard() {
    mLayout.setVisibility(RelativeLayout.INVISIBLE);
    mKLayout.setVisibility(RelativeLayout.INVISIBLE);

}

private void hideDefaultKeyboard() {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mEt.getWindowToken(), 0);
    //imm.hideSoftInputFromWindow(mKLayout.getApplicationWindowToken(), 0);

}

//get ids del xml y setOnClickListeners
private void setKeys() {
    mWindowWidth = getWindowManager().getDefaultDisplay().getWidth();
    mB[0] = (Button) findViewById(R.id.xCero);
    mB[1] = (Button) findViewById(R.id.xUno);
    mB[2] = (Button) findViewById(R.id.xDos);
    mB[3] = (Button) findViewById(R.id.xTres);
    mB[4] = (Button) findViewById(R.id.xCuatro);
    mB[5] = (Button) findViewById(R.id.xCinco);
    mB[6] = (Button) findViewById(R.id.xSeis);
    mB[7] = (Button) findViewById(R.id.xSiete);
    mB[8] = (Button) findViewById(R.id.xOcho);
    mB[9] = (Button) findViewById(R.id.xNueve);
    mB[10] = (Button) findViewById(R.id.xA);
    mB[11] = (Button) findViewById(R.id.xB);
    mB[12] = (Button) findViewById(R.id.xC);
    mB[13] = (Button) findViewById(R.id.xD);
    mB[14] = (Button) findViewById(R.id.xE);
    mB[15] = (Button) findViewById(R.id.xF);
    mB[16] = (Button) findViewById(R.id.xG);
    mB[17] = (Button) findViewById(R.id.xH);
    mB[18] = (Button) findViewById(R.id.xI);
    mB[19] = (Button) findViewById(R.id.xJ);
    mB[20] = (Button) findViewById(R.id.xK);
    mB[21] = (Button) findViewById(R.id.xL);
    mB[22] = (Button) findViewById(R.id.xM);
    mB[23] = (Button) findViewById(R.id.xN);
    mB[24] = (Button) findViewById(R.id.xENIE);
    mB[25] = (Button) findViewById(R.id.xO);
    mB[26] = (Button) findViewById(R.id.xP);
    mB[27] = (Button) findViewById(R.id.xQ);
    mB[28] = (Button) findViewById(R.id.xR);
    mB[29] = (Button) findViewById(R.id.xS);
    mB[30] = (Button) findViewById(R.id.xT);
    mB[31] = (Button) findViewById(R.id.xU);
    mB[32] = (Button) findViewById(R.id.xV);
    mB[33] = (Button) findViewById(R.id.xW);
    mB[34] = (Button) findViewById(R.id.xX);
    mB[35] = (Button) findViewById(R.id.xY);
    mB[36] = (Button) findViewById(R.id.xZ);
    mB[37] = (Button) findViewById(R.id.xPUNTO);
    mB[38] = (Button) findViewById(R.id.xPREGUNTA);
    mB[39] = (Button) findViewById(R.id.xEXCLAMACION);
    mBorrar = (Button) findViewById(R.id.xBorrar);
    mBSpace = (Button) findViewById(R.id.xSpace);
    mBack = (Button) findViewById(R.id.xBack);
    for (int i = 0; i < mB.length; i++)
        mB[i].setOnClickListener(this);
    mBorrar.setOnClickListener(this);
    mBSpace.setOnClickListener(this);
    mBack.setOnClickListener(this);


}

}

和布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/xMLayout"
android:background="#000000" android:layout_height="fill_parent"
android:focusable="true"><!-- android:orientation="vertical"  -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/xsubLayout"
    android:keepScreenOn="true"
    android:layout_height="fill_parent"><!-- android:orientation="vertical"  -->
    <EditText android:id="@+id/xEt" android:layout_width="fill_parent"
        android:focusableInTouchMode="true" android:layout_height="wrap_content" />
    <!--<EditText android:id="@+id/et1" android:layout_width="fill_parent"
        android:layout_below="@+id/xEt" android:layout_height="wrap_content" />-->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:id="@+id/xK1"
    android:layout_height="wrap_content"
    android:visibility="gone"> <!-- android:orientation="vertical"  -->
    <include android:id="@+id/xKeyBoard" layout="@layout/keyboard"></include>
</RelativeLayout>

键盘布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hebrwKeyboardView" android:layout_width="fill_parent"
android:layout_alignParentBottom="true" android:layout_below="@+id/xsubLayout"
android:orientation="vertical" android:background="#252625"
android:visibility="visible" android:layout_height="225sp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="225sp"
    android:orientation="vertical" android:layout_alignParentBottom="true"
    android:clipChildren="true">
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="225sp"
        android:padding="0sp">
        <TableRow android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="0sp">
            <LinearLayout android:baselineAligned="true"
                android:layout_width="fill_parent" android:layout_height="45sp"
                android:fitsSystemWindows="true">
                <Button android:soundEffectsEnabled="true" android:id="@+id/xCero"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="0" android:textColor="#000" android:tag="0"
                    android:padding="0sp" android:textStyle="bold" android:layout_gravity="center"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xUno"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:padding="0sp" android:textColor="#000" android:tag="1"
                    android:text="1" android:textStyle="bold" android:layout_gravity="center" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xDos"
                    android:layout_gravity="center" android:layout_width="32sp"
                    android:padding="0sp" android:layout_height="fill_parent"
                    android:text="2" android:tag="2" android:textStyle="bold"
                    android:textColor="#000" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xTres"
                    android:layout_width="32sp" android:layout_gravity="center"
                    android:layout_height="fill_parent" android:text="3" android:tag="3"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" /><!--  android:ellipsize="marquee" /> -->
                <Button android:soundEffectsEnabled="true" android:id="@+id/xCuatro"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:layout_gravity="center_horizontal" android:text="4"
                    android:tag="4" android:fitsSystemWindows="true"
                    android:textColor="#000" android:textStyle="bold"
                    android:ellipsize="marquee" android:padding="0sp"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xCinco"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:tag="5" android:layout_gravity="center" android:text="5"
                    android:fitsSystemWindows="true" android:textColor="#000" android:padding="0sp"
                    android:textStyle="bold" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xSeis"
                    android:layout_width="32sp" android:layout_gravity="center_horizontal"
                    android:layout_height="fill_parent" android:text="6" android:tag="6"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xSiete"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="7" android:fitsSystemWindows="true" android:tag="7"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xOcho"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="8" android:fitsSystemWindows="true" android:tag="8"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xNueve"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:textColor="#000" android:textStyle="bold" android:text="9"
                    android:fitsSystemWindows="true" android:tag="9" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
            </LinearLayout>
        </TableRow>
        <TableRow android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="0sp">
            <LinearLayout android:baselineAligned="true"
                android:layout_width="fill_parent" android:layout_height="45sp"
                android:fitsSystemWindows="true">
                <Button android:soundEffectsEnabled="true" android:id="@+id/xA"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="A" android:textColor="#000" android:tag="A"
                    android:padding="0sp" android:textStyle="bold" android:layout_gravity="center"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xB"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:padding="0sp" android:textColor="#000" android:tag="B"
                    android:text="B" android:textStyle="bold" android:layout_gravity="center" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xC"
                    android:layout_gravity="center" android:layout_width="32sp"
                    android:padding="0sp" android:layout_height="fill_parent"
                    android:text="C" android:tag="C" android:textStyle="bold"
                    android:textColor="#000" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xD"
                    android:layout_width="32sp" android:layout_gravity="center"
                    android:layout_height="fill_parent" android:text="D" android:tag="D"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" /><!--  android:ellipsize="marquee" /> -->
                <Button android:soundEffectsEnabled="true" android:id="@+id/xE"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:layout_gravity="center_horizontal" android:text="E"
                    android:tag="E" android:fitsSystemWindows="true"
                    android:textColor="#000" android:textStyle="bold"
                    android:ellipsize="marquee" android:padding="0sp"/>
                <Button android:soundEffectsEnabled="true" android:id="@+id/xF"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:tag="F" android:layout_gravity="center" android:text="F"
                    android:fitsSystemWindows="true" android:textColor="#000" android:padding="0sp"
                    android:textStyle="bold" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xG"
                    android:layout_width="32sp" android:layout_gravity="center_horizontal"
                    android:layout_height="fill_parent" android:text="G" android:tag="G"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:fitsSystemWindows="true" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xH"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="H" android:fitsSystemWindows="true" android:tag="H"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xI"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:text="I" android:fitsSystemWindows="true" android:tag="I"
                    android:textColor="#000" android:textStyle="bold" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xJ"
                    android:layout_width="32sp" android:layout_height="fill_parent"
                    android:textColor="#000" android:textStyle="bold" android:text="J"
                    android:fitsSystemWindows="true" android:tag="J" android:padding="0sp"
                    android:layout_gravity="center_horizontal" android:ellipsize="marquee" />
            </LinearLayout>
        </TableRow>
        ...
...
        <TableRow android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:fitsSystemWindows="true"
            android:orientation="horizontal">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="45sp" android:gravity="bottom"
                android:orientation="horizontal">
                <Button android:soundEffectsEnabled="true" android:id="@+id/xBorrar"
                    android:textColor="#000" android:textStyle="bold"
                    android:layout_width="115sp" android:layout_height="fill_parent"
                    android:tag="borrarTodo" android:text="BORRA_TODO" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xSpace"
                    android:textColor="#000" android:textStyle="bold"
                    android:layout_width="135sp" android:layout_height="fill_parent"
                    android:tag=" " android:text="|___ESPACIO___|" android:fitsSystemWindows="true" />
                <Button android:soundEffectsEnabled="true" android:id="@+id/xBack"
                    android:layout_width="70sp" android:layout_height="fill_parent"
                    android:textColor="#000" android:textStyle="bold" android:tag="back"
                    android:layout_gravity="center_horizontal|center_vertical|center"
                    android:fitsSystemWindows="true" android:text="BORRA" />
            </LinearLayout>
        </TableRow>
    </TableLayout>
</TableLayout>

该应用程序基于我从以下网址下载的示例:http: //tutorials-android.blogspot.com/2011/06/create-your-own-custom-keyboard-for.html

任何帮助都会很棒,我不知道现在还能尝试什么......

4

1 回答 1

1

我发现了问题所在。我使用的 AVD 的屏幕为 640x480,并使用标准布局 xml。我用来测试应用程序的物理设备是屏幕为 800x600 的平板电脑,因此针对没有所有按钮的 layout-large xml。

于 2012-05-12T15:44:18.443 回答