我需要一个假设有两个功能的按钮。
如果我点击一次,它将转到下一页。 如果我按住按钮,它将允许我编辑按钮上的文本。
按钮可以在点击它时转到下一页,但是如何实现允许我更改文本的第二个功能如果我按住按钮?
有人知道吗?
java代码
public class MainActivity extends Activity {
Button button1;
Button button2;
Button button3;
Handler h;
private Socket socket;
private boolean mInSettingsMode;
private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "192.168.43.83";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
// living button click start
button1 = (Button) findViewById(R.id.btnliving);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
h = new Handler();
h.postDelayed(irun, 0);
}
});
button2 = (Button) findViewById(R.id.btnbed);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
h = new Handler();
h.postDelayed(irun2, 0);
}
});
button3 = (Button) findViewById(R.id.btndin);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
h = new Handler();
h.postDelayed(irun3, 0);
}
});
}
Runnable irun = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, living.class);
startActivity(i);
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
};
Runnable irun2 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, bed.class);
startActivity(i);
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
};
Runnable irun3 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, din.class);
startActivity(i);
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
};
/*
* @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
* menu; this adds items to the action bar if it is present.
* getMenuInflater().inflate(R.menu.main, menu); return true; }
*/
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
// new Thread(new ClientThread()).start();
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/back1"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnliving"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light Control"
/>
<Button
android:id="@+id/btnbed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fan Control"
/>
<Button
android:id="@+id/btndin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Thermostat Control"
/>
</LinearLayout>
答案 0 :(得分:4)
您可以将第二个函数设置为setOnLongClickListener:
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
//change text here
return true;
}
});
并且第一个函数与setOnclickListener相同:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
答案 1 :(得分:1)
button.setOnTouchListener(new View.OnTouchListener() {
private static final long MAX_CLICK_TIME = 400;
long time = 0;
@Override
public boolean onTouch(View view, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
time = System.currentTimeMillis();
} else if (MotionEvent.ACTION_UP == event.getAction()) {
long pressingTime = System.currentTimeMillis() - time;
if (pressingTime > MAX_CLICK_TIME) {
// do change text logic
} else {
// show new page logic
}
}
return true;
}
});
如果要更改按钮的文本而不必等到用户将手指从按钮上移开,则可以将TimerTask.Code与TimerTask一起使用:
button.setOnTouchListener(new View.OnTouchListener() {
private static final long MAX_CLICK_TIME = 400;
long time = 0;
Timer timer = new Timer();
@Override
public boolean onTouch(View view, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
time = System.currentTimeMillis();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
// do change text logic
}
};
timer.schedule(timerTask, MAX_CLICK_TIME);
} else if (MotionEvent.ACTION_UP == event.getAction()) {
long pressingTime = System.currentTimeMillis() - time;
if (pressingTime <= MAX_CLICK_TIME) {
timer.cancel();
// show new page logic
}
}
return true;
}
});
我没有测试,但应该工作。无论如何,这个想法应该是明确的。