当前位置: 首页 > 面试题库 >

Android:连续[一遍又一遍]轻按一到六个按钮,将不同的结果组合在一起

濮阳旭东
2023-03-14
问题内容

我决定开发一个Android应用程序,该应用程序使用的技术与以前看到的应用程序非常相似。我想将多个按钮按下组合在一起,以等同于不同的不同文本结果。

六个点-盲文应用程序(实际使用的应用程序)

我正在制作的这款本机盲文应用程序具有6个不同的按钮,我希望每种独特的组合都能带给我不同的字母。例如:我想按一下按钮1来简单地给我带来字母“
A”。然后,连续按下按钮1和按钮2会给我带来字母“ C”。我希望这6个按钮的每种不同按钮组合能带给我一封单独的信。

精通Java的人可以解释一下如何完成吗?如何串入多个按钮以使我得到不同的结果?谢谢您的帮助。

盲文字母

我在Java上的代码:

        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.keyboard);

          Window window = this.getWindow();
          window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
          window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
          window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

          // Init GUI
          txtMessage = (EditText) findViewById(R.id.txtMesssage);
          Button buttonOne = (Button) findViewById(R.id.block1);
          Button buttonTwo = (Button) findViewById(R.id.block2);
          Button buttonThree = (Button) findViewById(R.id.block3);
          Button buttonFour = (Button) findViewById(R.id.block4);
          Button buttonFive = (Button) findViewById(R.id.block5);
          Button buttonSix = (Button) findViewById(R.id.block6);



          // Attached Click Listener
          btnSend.setOnClickListener(this);

          buttonOne.setOnClickListener(this);
          buttonTwo.setOnClickListener(this);
          buttonThree.setOnClickListener(this);
          buttonFour.setOnClickListener(this);
          buttonFive.setOnClickListener(this);
          buttonSix.setOnClickListener(this);

    }
@Override
    public void onClick(View v) {

        }

        switch (v.getId()) {
        case R.id.block1:

             break;
        case R.id.block2:

             break;
        case R.id.block3:

            break;
        case R.id.block4:

            break;
        case R.id.block5:

            break;
        case R.id.block6:

            break;
        }
     txtMessage.setText();
    }

    //functions below.
    ....     ....
    ...       ...
    ..         ..
    .     O     .
    ..         ..
    ...       ...
    ....     ....

在我的XML布局上 keyboard.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@color/lightgrey">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

<EditText
    android:id="@+id/txtMesssage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:hint="Enter Message"
    android:textColor="@color/darkbrown" >
</EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"

    android:orientation="horizontal" >

    <Button
        android:id="@+id/block1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_weight="1.0"
        android:text="Button one" />

    <Button
        android:id="@+id/block2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/blue"
        android:text="Button two" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/block3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/bg_gradient_end"
        android:text="Button three" />

    <Button
        android:id="@+id/block4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/darkgrey"
        android:text="Button four" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/block5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/lightgrey"
        android:text="Button five" />

    <Button
        android:id="@+id/block6"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/bg_gradient_start"
        android:text="Button six" />

</LinearLayout>

到目前为止,我编写的代码无法正常运行,因此现在将switch语句留空。请更正我的代码或为我提供帮助。谢谢

我的计时器代码:

            Timer processTimer = new Timer();
        processTimer.schedule(new TimerTask() {
            public void run() {    
                processInput();    
            }

            private void processInput() {
                // TODO Auto-generated method stub
                map.put(getString(R.id.block1), "A");
                map.put(getString(R.id.block1) + getString(R.id.block2), "C");



            }
        }, 500); // Delay before processing.

    processTimer.cancel();

    processTimer.schedule(new TimerTask() {
            public void run() {    
                processInput();    
            }

            private void processInput() {
                // TODO Auto-generated method stub
                map.get(R.id.block1);
                map.get(R.id.block1+R.id.block2);
                //this.close();


            }
        }, 500);

这样对吗?


问题答案:

我尝试使用类似于aptyp建议的方法进行编码:

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
static long DELAY_TIME_INPUT = 500;
static int INPUT_TYPE_NORMAL = 0;
static int INPUT_TYPE_CAP = 1;
static int INPUT_TYPE_NUM = 2;

TextView txtMessage;
int[] mAlphabetTable1 = new int[]{1, 3, 9, 25, 17, 11, 27, 19, 10, 26,
                                5, 7, 13, 29, 21, 15, 31, 23, 14, 30,
                                37, 39, 58, 45, 61, 53};
int[] mSymbolTable1 = new int[]{2, 6, 4, 18, 36, 40, 50, 22, 38, 52, 54, 12};
/* Note: The value used below {8, 16, 20, 24} are just an example.
    I choose these values because they are not defined on the above tables.
  */
int[] mSpecialTable1 = new int[]{8, 16, 20, 24};

// char[] mAlphabetTable2 = new char[]{};
char[] mNumberTable2 = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
char[] mSymbolTable2 = new char[]{',', ';', '\'', ':','-', '.', '.', '!', '“', '”','(','/'};
int mCurrentAlphabet = 0;
int mCurrentInputType = 0;
long mLastTimeStamp;

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

    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    // Init GUI
    txtMessage = (TextView) findViewById(R.id.txtMesssage);
    Button buttonOne = (Button) findViewById(R.id.block1);
    Button buttonTwo = (Button) findViewById(R.id.block2);
    Button buttonThree = (Button) findViewById(R.id.block3);
    Button buttonFour = (Button) findViewById(R.id.block4);
    Button buttonFive = (Button) findViewById(R.id.block5);
    Button buttonSix = (Button) findViewById(R.id.block6);
    // Attached Click Listener
    buttonOne.setOnClickListener(this);
    buttonTwo.setOnClickListener(this);
    buttonThree.setOnClickListener(this);
    buttonFour.setOnClickListener(this);
    buttonFive.setOnClickListener(this);
    buttonSix.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.block1: mCurrentAlphabet |=1; break;
        case R.id.block2: mCurrentAlphabet |=2; break;
        case R.id.block3: mCurrentAlphabet |=4; break;
        case R.id.block4: mCurrentAlphabet |=8; break;
        case R.id.block5: mCurrentAlphabet |=16; break;
        case R.id.block6: mCurrentAlphabet |=32; break;
    }
    view.setBackgroundColor(Color.BLACK);
    Button btView = (Button) view;
    btView.setTextColor(Color.WHITE);
    mLastTimeStamp = System.currentTimeMillis();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            long currentTimeStamp = System.currentTimeMillis();
            if(currentTimeStamp - mLastTimeStamp > DELAY_TIME_INPUT){
                genNewBrailleAlphabet();
            }
        }
    }, DELAY_TIME_INPUT + 10);
}

public void genNewBrailleAlphabet(){
    if(mCurrentAlphabet == 32 || mCurrentAlphabet == 60){ // Check if input is Cap or Num sign?
        if(mCurrentAlphabet == 32){ // Input is Cap sign.
            mCurrentInputType = INPUT_TYPE_CAP;
            TextView txtCap = (TextView) findViewById(R.id.cap);
            txtCap.setBackgroundColor(Color.GREEN);
        } else { // Input is Num sign.
            TextView txtNum = (TextView) findViewById(R.id.num);
            if(mCurrentInputType == INPUT_TYPE_NUM){
                mCurrentInputType = INPUT_TYPE_NORMAL; // Turn off Num sign.
                txtNum.setBackgroundColor(Color.TRANSPARENT);
            } else {
                mCurrentInputType = INPUT_TYPE_NUM; // Turn on Num sign.
                txtNum.setBackgroundColor(Color.GREEN);
            }
        }
    } else { // Input is not Cap or Num sign.
        byte currentAlphabetIndex = -1;
        char newAlphabet = 0;
        for (int i = 0; i < mAlphabetTable1.length; i++) {
            if (mAlphabetTable1[i] == mCurrentAlphabet) {
                currentAlphabetIndex = (byte) i;
                break;
            }
        }
        if(currentAlphabetIndex != -1) { // Check if input is Numbers or Alphabets?
            if (mCurrentInputType == INPUT_TYPE_NUM) { // Input is Numbers.
                if(currentAlphabetIndex < 10) {
                    newAlphabet = mNumberTable2[currentAlphabetIndex];
                }
            } else if (mCurrentInputType == INPUT_TYPE_CAP) // Input is Alphabets.
                newAlphabet = (char) (currentAlphabetIndex + 'A');
            else newAlphabet = (char) (currentAlphabetIndex + 'a');

            String msg = txtMessage.getText().toString() + newAlphabet;
            txtMessage.setText(msg);
        } else { // Input is not Numbers or Alphabets.
            for (int i = 0; i < mSymbolTable1.length; i++) {
                if (mSymbolTable1[i] == mCurrentAlphabet) {
                    currentAlphabetIndex = (byte) i;
                    break;
                }
            }
            if(currentAlphabetIndex != -1) { // Check if input is Punctuations?
                newAlphabet = mSymbolTable2[currentAlphabetIndex];
                if(currentAlphabetIndex == 8){ // Open Quote, Question Mark have the same pattern.
                    String tmpString = txtMessage.getText().toString();
                    if(tmpString.length() > 0 && !tmpString.endsWith(" ")){
                        // Last typed alphabet is not space, so this is Question Mark.
                        newAlphabet = '?';
                    }
                }
                String msg = txtMessage.getText().toString() + newAlphabet;
                txtMessage.setText(msg);
            } else { // Input is not Punctuations, so it is Special Action or undefined.
                for (int i = 0; i < mSpecialTable1.length; i++) {
                    if (mSpecialTable1[i] == mCurrentAlphabet) {
                        currentAlphabetIndex = (byte) i;
                        break;
                    }
                }
                if(currentAlphabetIndex != -1) { // Check if input is Special Action?
                    String msg = txtMessage.getText().toString();
                    // Input is Special Action
                    switch (currentAlphabetIndex) {
                        case 0: // Change focus here
                            // Change focus code
                            /* if (txtNumber.hasFocus()) {
                                txtMessage.requestFocus();
                            } else {
                                txtNumber.requestFocus();
                            } */
                            break;
                        case 1: // BackSpace
                            msg = msg.substring(0, msg.length() - 1);
                            txtMessage.setText(msg);
                            break;
                        case 2: // Space
                            msg = msg + " ";
                            txtMessage.setText(msg);
                            break;
                        case 3: // New Line
                            msg = msg + "\n";
                            break;
                    }
                    txtMessage.setText(msg);
                } else { // Input not defined.
                    Toast.makeText(getApplicationContext(), "Clicked button combination not defined!!", Toast.LENGTH_SHORT).show();
                }
            }
        }

        if(mCurrentInputType == INPUT_TYPE_CAP){
            TextView txtCap = (TextView) findViewById(R.id.cap);
            txtCap.setBackgroundColor(Color.TRANSPARENT);
            mCurrentInputType = INPUT_TYPE_NORMAL;
        }
    }
    // Reset button views ana variable for next alphabet.
    Button buttonOne = (Button) findViewById(R.id.block1);
    Button buttonTwo = (Button) findViewById(R.id.block2);
    Button buttonThree = (Button) findViewById(R.id.block3);
    Button buttonFour = (Button) findViewById(R.id.block4);
    Button buttonFive = (Button) findViewById(R.id.block5);
    Button buttonSix = (Button) findViewById(R.id.block6);
    buttonOne.setBackgroundColor(Color.WHITE);
    buttonTwo.setBackgroundColor(Color.WHITE);
    buttonThree.setBackgroundColor(Color.WHITE);
    buttonFour.setBackgroundColor(Color.WHITE);
    buttonFive.setBackgroundColor(Color.WHITE);
    buttonSix.setBackgroundColor(Color.WHITE);
    buttonOne.setTextColor(Color.BLACK);
    buttonTwo.setTextColor(Color.BLACK);
    buttonThree.setTextColor(Color.BLACK);
    buttonFour.setTextColor(Color.BLACK);
    buttonFive.setTextColor(Color.BLACK);
    buttonSix.setTextColor(Color.BLACK);
    mCurrentAlphabet = 0;
}}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    android:layout_weight="4.0"
    android:background="@color/lightgrey"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtMesssage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textColor="@color/darkbrown" >
        </TextView>
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal"
    android:baselineAligned="false"
    android:layout_marginTop="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="#000000"
        android:orientation="vertical" >

        <TextView
            android:text="CAP"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/cap"
            android:layout_weight="1.3"
            android:gravity="center"
            android:textSize="20sp" />

        <Button
            android:id="@+id/block1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:text="Button one" />

        <Button
            android:id="@+id/block2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:text="Button two" />

        <Button
            android:id="@+id/block3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:text="Button three" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="vertical"
        android:background="#000000">

        <TextView
            android:text="Num"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/num"
            android:layout_weight="1.3"
            android:gravity="center"
            android:textSize="20sp" />

        <Button
            android:id="@+id/block4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:text="Button four" />

        <Button
            android:id="@+id/block5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:text="Button five" />

        <Button
            android:id="@+id/block6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#ffffff"
            android:text="Button six" />

    </LinearLayout>

</LinearLayout>

请注意,我使用的是处理程序,而不是计时器。希望对您有所帮助!



 类似资料:
  • 本文向大家介绍C#一遍又一遍地生成相同的随机数序列,包括了C#一遍又一遍地生成相同的随机数序列的使用技巧和注意事项,需要的朋友参考一下 示例 当创建Random具有相同种子的实例时,将生成相同的编号。 输出:            

  • 这是我的问题: 我想在一个活动中实现两个按钮。一个是计算BMI,另一个是检查时间。我希望用户能在同一个活动中看到两个按钮,这也取决于他们想做什么。(这些功能不是同时工作的,而是分开的。另外,我在这两个活动中都实现了返回主页的按钮。) 在Android模拟器中,消息来自“应用程序继续停止”。 以下是主要活动。java文件: 谢谢! 这些消息是我按下导致应用程序停止的按钮后的stacktrace:

  • 我正在使用iSpeech的API用于TTS,在他们的示例android项目中,他们有两个带有两个OnClickListener的按钮,一个用于开始演讲,一个用于停止演讲。我想把它保持在一个按钮上,以便在android屏幕上有更多的空间,这样,如果语音停止,按下按钮就会开始,如果语音开始,按下按钮就会停止。 有很多类似的问题,答案表明最好保持一个监听器一个按钮,但我不太满意,因为这个问题是在多个监听

  • 在一段时间的循环中,我使循环在一次无效输入后不会返回有效的答案,并重复“错误!无效的客户类型。再试一次。”一遍又一遍,直到我关闭程序。如果我第一次输入R或C作为输入,它会正常工作。当我输入其他任何东西时,我会得到错误信息“错误!无效的客户类型。再试一次。”就像我应该是故意的一样。然而,在输入r或c错误之后,我又会再次出现错误,我所做的任何输入都会一遍又一遍地返回错误信息,直到我关闭程序。有人能告诉

  • 我在使用Android GridView时遇到了奇怪的问题。我创建了一个3x4的网格,并在其中插入了按钮。我希望当用户单击按钮时,按钮的背景发生变化。这对于除了第一个按钮(索引为0 -左上角的那个)之外的所有按钮都很好。无论我做什么,OnClick事件侦听器都不会为那个按钮触发。 这是我创建视图的代码: 如果我换掉 具有 然后onClick()被触发,但背景仍然没有改变。所有其他按钮都按预期工作。

  • 问题内容: 有没有那么冗长的替代方案: 我想出了这个: 这节省了一个缩进,但仍然很丑陋。 我希望看起来像这样的伪代码: 有没有类似的东西存在? 问题答案: 我认为您正在寻找ndenumerate。 关于性能。它比列表理解要慢一些。 如果您担心性能,可以通过查看实现来进一步优化,该实现有两件事,转换为数组并循环。如果知道有数组,则可以调用平面迭代器的属性。