两个带有 onClick 的按钮在同一个地方
two buttons with onClick in same place
我需要做两个按钮 onClick.That 两个按钮必须位于同一个 place.one 按钮用于语音记录 onClick 的启动功能和另一个按钮用于停止功能 onClick.is可能吗?如果我得到任何建议,这将对 me.Thank 你有所帮助。
我搜索了很多 SO post 但 tutorial.I 没找到。
试试这样的东西:
public class MyActivity extends Activity {
Button buttonStart;
Button buttonStop;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//for example mediaPlayer.start(); if you have a media player
}
});
buttonStop.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//do what you want - for example mediaPlayer.stop();
}
});
}
}
发表评论后:然后使用单个按钮检查它是否正在录制,例如:
public class MyActivity extends Activity {
Button button;
boolean isRecording = false;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
button = (Button) findViewById(R.id.myButton);
button.setText("Start");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if(isRecording == fasle){
//start recording
isRecording = true; //set the variable to know that it is recording
button.setText("Stop");
} else {
//stop recording
isRecording = false; //set the variable to know that it is not recording
button.setText("Start");
}
}
});
}
}
如果您想要完全相同的(即 2 个按钮)
您可以使用相对布局,并在其中添加两个按钮。
您可以像这样以编程方式显示/隐藏它们:
public void onClick(View v)
{
switch(v.getId()) {
case R.id.btn1:
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.VISIBLE);
break;
case R.id.btn2:
btn2.setVisibility(View.GONE);
btn1.setVisibility(View.VISIBLE);
break;
}
}
否则,you can even use one button for both the functionalities (start and stop)
为-
public void onClick(View v)
{
if(btn1.getText().toString().equalsIgnoreCase("start"))
{
btn1.setText("stop");
mp2.start();
musicPlaying = true;
}
else if(btn1.getText().toString().equalsIgnoreCase("stop"))
{
btn1.setText("start");
mp2.stop();
musicPlaying = false;
}
}
我需要做两个按钮 onClick.That 两个按钮必须位于同一个 place.one 按钮用于语音记录 onClick 的启动功能和另一个按钮用于停止功能 onClick.is可能吗?如果我得到任何建议,这将对 me.Thank 你有所帮助。
我搜索了很多 SO post 但 tutorial.I 没找到。
试试这样的东西:
public class MyActivity extends Activity {
Button buttonStart;
Button buttonStop;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//for example mediaPlayer.start(); if you have a media player
}
});
buttonStop.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//do what you want - for example mediaPlayer.stop();
}
});
}
}
发表评论后:然后使用单个按钮检查它是否正在录制,例如:
public class MyActivity extends Activity {
Button button;
boolean isRecording = false;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
button = (Button) findViewById(R.id.myButton);
button.setText("Start");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if(isRecording == fasle){
//start recording
isRecording = true; //set the variable to know that it is recording
button.setText("Stop");
} else {
//stop recording
isRecording = false; //set the variable to know that it is not recording
button.setText("Start");
}
}
});
}
}
如果您想要完全相同的(即 2 个按钮)
您可以使用相对布局,并在其中添加两个按钮。
您可以像这样以编程方式显示/隐藏它们:
public void onClick(View v)
{
switch(v.getId()) {
case R.id.btn1:
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.VISIBLE);
break;
case R.id.btn2:
btn2.setVisibility(View.GONE);
btn1.setVisibility(View.VISIBLE);
break;
}
}
否则,you can even use one button for both the functionalities (start and stop)
为-
public void onClick(View v)
{
if(btn1.getText().toString().equalsIgnoreCase("start"))
{
btn1.setText("stop");
mp2.start();
musicPlaying = true;
}
else if(btn1.getText().toString().equalsIgnoreCase("stop"))
{
btn1.setText("start");
mp2.stop();
musicPlaying = false;
}
}