我决定开发一款Android应用程序,该应用程序使用的技术与我之前看过的应用程序非常相似.我想将多个按钮组合在一起,以等同于不同的文本结果.
Six dots – braille application (actual application to use)
这个我正在制作的本地盲文应用程序有6个不同的按钮,我希望每个独特的组合带给我不同的字母.例如:我想按下按钮1来简单地给我带来字母“A”.然后按下按钮1和按钮2不断给我带来字母“C”.我希望这6个按钮的每个不同的按钮组合给我一个单独的字母.
精通Java的人能否解释一下如何做到这一点?如何在多个按钮按下字符串以使我得到不同的结果?谢谢您的帮助.
Braille alphabet
我在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 Layout 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;@OverrIDeprotected 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);}@OverrIDepublic 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>
请注意我使用的是处理程序而不是计时器.希望它有所帮助!
总结以上是内存溢出为你收集整理的java – Android:连续点击一到六个按钮[一个接一个]将不同的结果串在一起全部内容,希望文章能够帮你解决java – Android:连续点击一到六个按钮[一个接一个]将不同的结果串在一起所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)