答案 0 :(得分:1)
所以我一直在挖掘这个主题并想到一些要记住的事情。
// All of this inside your OnClick-event
// If (both buttons are clicked)
{
Toast.makeText(getApplicationContext(), "Show this message", Toast.LENGTH_SHORT);
}
if(toast != null)
toast.show();
// End your OnClick-event.
需要设置上述设置。
您可能还想了解的是Android Multitouch。
对于如此广泛的问题,我无法从头开始给你一个例子,但我会给你一些有用的链接,以帮助你入门。
1. Android work multitouch button [StackOverflow]
2. How to code for multitouch [StackOverflow]
3. Handling Multi-Touch Gestures [developer.android.com]
4. How to use Multi-touch in Android 2 [zdnet.com]
我认为你正在寻找类似第5个链接的东西。同样,我不会为了它而在这里发布代码。
因此,在您实现了上面的第一部分后,您只需要将以下代码添加到该事件中(您已经检查过同时点击了两个按钮)。
spinner.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, your_arraylist));
答案 1 :(得分:0)
boolean button1_clicked=false, button2_clicked=false;
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button1_clicked =true;
if(button1_clicked && button2_clicked){
Toast.makeText(context, "Toast", Toast.LENGTH_SHORT).show();
button1_clicked =false;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button1_clicked =false;
}
}, 500);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button2_clicked =true;
if(button1_clicked && button2_clicked){
Toast.makeText(context, "Toast", Toast.LENGTH_SHORT).show();
button2_clicked =false;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button2_clicked =false;
}
}, 500);
}
});
我认为这个处理程序有助于在500毫秒后重置变量。
答案 2 :(得分:0)
我用这种方式:
public class MainActivity extends AppCompatActivity
{
private long start;
private long finish;
private boolean ciclo=true;
private Activity activity;
private boolean show;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
}
public void onClick(View arg0)
{
new MultiplyTask().execute();
switch(arg0.getId())
{
case R.id.button:
start = System.currentTimeMillis();
break;
case R.id.button2:
finish = System.currentTimeMillis();
}
}
public class MultiplyTask extends AsyncTask<String,String,String>
{
@Override
public void onPreExecute()
{
super.onPreExecute();
}
@Override
public void onPostExecute(String s)
{
super.onPostExecute(s);
if(show)
{
Toast.makeText(activity, "Yes", Toast.LENGTH_LONG).show();
ciclo = true;
show=false;
}
}
@Override
public String doInBackground(String... params)
{
while(ciclo)
{
if(Math.abs(finish-start)<50)
{
show=true;
ciclo=false;
}
}
return null;
}
}
}
布局必须是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="69dp"
android:layout_marginStart="69dp"
android:layout_marginTop="215dp"
android:id="@+id/button"
android:onClick="onClick" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:onClick="onClick"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="69dp"
android:layout_marginStart="69dp" />
</RelativeLayout>