- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我决定开发一款 Android 应用,它使用的技术与我之前见过的应用非常相似。我想将多个按钮按下串在一起以等同于不同的不同文本结果。
Six dots - braille application (actual application to use)
我正在制作的这个本地盲文应用程序有 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 建议的方法进行编码:
主要 Activity .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>
请注意,我使用的是处理程序而不是计时器。希望对您有所帮助!
关于java - Android:连续点击一到六个按钮 [一个接一个] 将不同的结果串在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41514223/
我想要的是能够在输入获得焦点或失去焦点时执行某些操作(两个事件)。 我尝试了以下方法,但这按事件单独工作(单独编码时):仅在焦点上,或仅在失去焦点时。 另外,我希望它尽可能跨平台(包括触摸设备),这是
我分别研究了TableView的Filtering和Pagination。 过滤: this帖子帮助我满足了我的需要 分页: this , this帖子也帮助了我 我想像这样将它们组合在一起: 详情-
我是 TDD 方法的新手,所以我想知道是否有人经历过这种机智可以启发我一点。我想获得一些关于如何一起使用 UML 和 TDD 方法的线索。 我已经习惯了:用 UML 设计 --> 生成骨架类(然后保持
我尝试使用入口点和 cmd 设置 Docker。 FROM debian:stretch RUN apt-get update && \ apt install gnupg ca-certificat
我想要一个 Class 对象,但我想强制它所代表的任何类扩展类 A 并实现接口(interface) B。 我能做到: Class 或者: Class 但我不能两者兼得。有办法做到这一点吗? 最佳答案
我是 Rubymine 的长期用户。 Rubymine 非常适合基于 html 的 Rails 应用程序,但我现在正在做更多的 SPA 客户端工作(例如 javascript/react)。我发现我真
我注意到我使用的某个脚本依赖于原型(prototype)。 (Lightbox 2) 它会与 jQuery 在同一页面上一起工作吗?有没有办法确保它们不冲突? 最佳答案 可以,但你需要采取 speci
我需要对表中显示的数据进行分页并通过 ajax 调用获取它 - 这是我通过使用具有以下配置的 dataTables 插件来完成的 - bServerSide : true; sAjaxSource :
我是 gtk 新手,所以想知道在 C 语言中归档和 gtk 是否可以一起使用?例如,我可以从 .txt 文件中读取,然后在相同的代码中使用 gtk 在标签或其他内容中显示它吗?如果是,怎么办? 谢谢!
有没有人设法得到Bck2Brwsr最近与 Java 8/JavaFX 8 一起工作?有没有兼容的机会?我找不到太多关于它的信息,也没有一个好的起点。使用给定的 Maven archetype我遇到了几
在我的应用程序中,用户通过 openid(与 stackoverflow 相同)登录/注销。 我想通过 oauth 向第三方应用程序开放我的应用程序。 如何创建我的 openid-consumer 应
我在启动和运行 Hibernate 和 Spring 时遇到一些问题。我有一个网络服务器项目,它使用了其他几个具有持久实体的项目。我遇到的问题是,对于存储在 WEB-INF/libs 内的另一个 ja
我有 @ControllerAdvice 类,它处理一组异常。我们还有一些其他异常,这些异常用 @ResponseStatus 注释进行注释。为了结合这两种方法,我们使用博客文章中描述的技术:http
我想在屏幕上使用进度条而不是 progressDialog。 我在我的 XML View 文件中插入了一个进度条,我想让它在加载时显示并在不加载时禁用它。 所以我使用的是可见的,但它发生了,所以其余的
CREATE TABLE `users` ( `id` int(11) AUTO_INCREMENT, `academicdegree` varchar(255),
IN() 中使用的查询返回:1, 2。然而,整个查询返回 0 行,这是不可能的,因为它们存在。我在这里做错了什么? SELECT DISTINCT li.auto_id FROM links
亲们, 我如何在使用 Jade 生成的表单上实现 jQuery 样式?我想做的是美化 表单并使它们可点击。我在 UI 方面很糟糕。期间。 我如何在表单上实现这个可选择的方法? http://jquer
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我可以: auto o1 = new Content; 但不能: std::shared_ptr o1(new Content); std::unique_ptr o1(new Content); 我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我是一名优秀的程序员,十分优秀!