当软键盘处于活动状态时,设置文本不起作用

时间:2012-10-22 03:27:19

标签: java android keyboard sleep

我正在编写一个Android应用程序,它会显示一小段数字的字符串。然后,用户将在文本字段中输入这些数字,以查看他们在该闪存中能够读取的数量。但是,当键盘打开时,按下触发按钮时不会出现数字串。为什么这样,我该怎么做才能解决它?

这是java代码:

package com.example.test;

import java.util.*;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

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

    public void ButtonOnClick(View v){
        Random rand = new Random();

        final Handler mHandler = new Handler();

        String number = new String();
        for(int i=0; i < 9; i++){
            int num = rand.nextInt(9);
            number = number + Integer.toString(num);
        }

        final TextView mTextView = (TextView) findViewById(R.id.textview1);
        mTextView.setText(number);

        final Runnable makeTextDisapear = new Runnable() {
            public void run() {
                mTextView.setText(null);

                }
            };

            mHandler.postDelayed(makeTextDisapear , 1);

    }

}

编辑:我通过在应用程序中制作屏幕键盘来解决这个问题,但我更喜欢使用系统键盘,所以我仍然对答案感兴趣。

1 个答案:

答案 0 :(得分:1)

首先:在使用您的代码时请注意you will only get digits from 0-8 (not a 9)

我为您构建了一些示例代码并对其进行了测试。这运行并执行您描述的内容。它会在适当的时刻隐藏键盘,仅在单击EditText时显示。它不会分散对数字的注意力。我希望这可以帮助你。请尝试代码。

MainActivity.java

package com.example.exampleshowdigits;

import java.util.Random;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity implements OnClickListener {

    private Button mButton;
    private TextView mTextView1;
    private TextView mTextView2;
    private EditText mEditText;
    private boolean numbersShown;
    private int[] numbers;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button)findViewById(R.id.button1);
        mTextView1 = (TextView)findViewById(R.id.textview1);
        mTextView2 = (TextView)findViewById(R.id.textview2);
        mEditText = (EditText)findViewById(R.id.edittext1);
        mButton.setOnClickListener(this);
        InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(mEditText, 0);
        numbersShown = false;
        numbers = new int[9];
    }

    @Override
    public void onClick(View v) {
        if(!numbersShown) {
            Random rand = new Random();
            String number = "";
            for(int i = 0; i < 9; i ++) {
                int num = rand.nextInt(10); //assuming you want 0-9
                numbers[i] = num;
                number += Integer.toString(num); //same as number = number + Integer.toString(num);
            }
            mTextView1.setText(number);
            SystemClock.sleep(500);
            final Runnable makeTextDisapear = new Runnable() {
                public void run() {
                    mTextView1.setText("");
                    mButton.setText(getString(R.string.checkresult));
                    mTextView2.setText(getString(R.string.whatdidyousee));
                    mEditText.setText("");
                    numbersShown = true;
                }
            };
            Handler mHandler = new Handler();
            mHandler.postDelayed(makeTextDisapear, 1000);
        }
        else {
            mButton.setText(getString(R.string.shownumbers));
            numbersShown = false;
            int correct = 0;
            String numbersEntered = mEditText.getText().toString();
            for(int i = 0; i < numbersEntered.length(); i++) {
                int num = Character.getNumericValue(numbersEntered.charAt(i));
                if(num == numbers[i]) {
                    correct++;
                }
            }
            mTextView2.setText(correct + " " + getString(R.string.correct));
        }
    }
}

activity_main.xml

<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" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/shownumbers"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp" />
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/button1" />
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginTop="15dp"
        android:layout_below="@id/textview1" />
    <EditText
        android:id="@+id/edittext1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:maxLength="9"
        android:singleLine="true"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/textview2" />
</RelativeLayout>

strings.xml

<string name="shownumbers">Show numbers</string>
<string name="checkresult">Check result</string>
<string name="whatdidyousee">What did you see? Enter here:</string>
<string name="correct">out of 9 correct!</string>