【发布时间】:2017-01-06 20:57:28
【问题描述】:
我决定开发一款 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);
这对吗?
【问题讨论】:
-
你打算实现一个“进入”按钮吗? (注册您的 6 个按钮的序列)还是您希望应用在暂停后自动接受序列?
-
@TWL 您好,您好,无需输入按钮。我希望应用程序在暂停后自动接受序列。 :)
-
二进制和呢?按钮 1 = 100000b,按钮 2 = 010000,按钮 3 = 001000b,按钮 4 = 000100b,按钮 5 = 000010 和按钮 6 = 000001。当按下按钮 1 和按钮 2 时,只需添加 100000b 和 010000b,结果为 110000b。保持不变:110000b = 'c'。按钮 1 + 按钮 4 是 100000b + 000100b = 100100b = 'e'。你知道我的意思吗?
-
@aptyp 天哪,我没有关注.. 我真的不明白,对不起,我真的不精通 java 编程。但这听起来很酷,但是如果我必须从 A-Z 字母工作会有什么冲突吗? 编辑:我不认为如果我必须从 A-Z 生成不起作用的字母..
-
@aptyp 这有点工作,但不允许在序列中使用双按钮,因为您基本上只是对数据进行或处理。您也无法分辨首先是什么按钮。所以 button1 然后 button2 == button2 然后 button1。这不好。
标签: java android eclipse function switch-statement