具有两个功能的Android按钮 [英] Android button having two functions
本文介绍了具有两个功能的Android按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要有一个假定具有两个功能的按钮.
I need to have a button that is suppose to have two functions.
如果我点击一次,它将转到下一页.如果我按住按钮,它将允许我编辑按钮上的文本.
If I tap on it once, it will go to the next page. If I hold down on the button, it will allow me to edit the text on the button.
点击该按钮可以转到下一页,但是如果我按住该按钮,该如何实现允许我更改文本的第二个功能呢?
The button is able to go to the next page upon tapping on it, but how do I implement the second function which allows me to change the text If I hold down the button?
有人知道吗?
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>
推荐答案
您可以将第二个功能设置为setOnLongClickListener:
You can set second function as a setOnLongClickListener as:
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
//change text here
return true;
}
});
第一个函数与setOnclickListener相同:
and first function is same setOnclickListener as:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
检查此文档
这篇关于具有两个功能的Android按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文