標題:
OnLongClickListener
[打印本頁]
作者:
周政輝
時間:
2018-9-29 12:55
標題:
OnLongClickListener
本帖最後由 周政輝 於 2018-10-6 11:49 編輯
以 OnClickListener 實作每點擊一次後退鍵減少一字元;以 OnLongClickListener 實作當按住後退鍵超過一秒則清空 TextView 上所有文字內容。
[attach]4880[/attach]
本帖隱藏的內容需要回復才可以瀏覽
作者:
黃茂勛
時間:
2018-9-29 21:55
[attach]4882[/attach]
新增代碼:
public void delete(View view) {
//show.setText(show.getText().subSequence(0,show.getText().length()-1)); 字串切割法
if(show.getText().equals("")) //防止空按錯誤
{
show.setText("");
}else {
st.pop(); //stak堆疊法
String output = "";
for (String str : st) {
output += str;
}
show.setText(output);
}
}
Button.OnLongClickListener longClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
show.setText("");
st.clear();
return true;
}
};
複製代碼
作者:
陳思惟
時間:
2018-10-6 11:13
本帖最後由 陳思惟 於 2018-10-6 11:14 編輯
package com.example.student.myapplication;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static android.Manifest.permission.*;
public class MainActivity extends AppCompatActivity {
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn10;
Button btn11;
Button call;
Button back;
TextView number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button tmp = (Button) findViewById(v.getId());
number.setText(number.getText().toString() + tmp.getText().toString());
}
};
Button.OnLongClickListener a01 = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
};
private void Init() {
btn0 = (Button) findViewById(R.id.button0);
btn0.setOnClickListener(listener);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(listener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(listener);
btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(listener);
btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(listener);
btn5 = (Button) findViewById(R.id.button5);
btn5.setOnClickListener(listener);
btn6 = (Button) findViewById(R.id.button6);
btn6.setOnClickListener(listener);
btn7 = (Button) findViewById(R.id.button7);
btn7.setOnClickListener(listener);
btn8 = (Button) findViewById(R.id.button8);
btn8.setOnClickListener(listener);
btn9 = (Button) findViewById(R.id.button9);
btn9.setOnClickListener(listener);
btn10 = (Button) findViewById(R.id.button10);
btn10.setOnClickListener(listener);
btn11 = (Button) findViewById(R.id.button11);
btn11.setOnClickListener(listener);
call = (Button) findViewById(R.id.PhoneCall);
number = (TextView) findViewById(R.id.Number);
back=(Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!number.getText().toString().equals(""))
number.setText(number.getText().toString().substring(0, number.getText().toString().length()-1));
}
});
back.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick(View v) {
number.setText("");
return true;
}
});
}
public void PhoneCall(View view) {
try {
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:" + number.getText().toString()));
//startActivity(dial);
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(dial);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
}catch(Exception e)
{
number.setText(e.toString());
}
}
/*public void Back(View view) {
if(!number.getText().toString().equals(""))
number.setText(number.getText().toString().substring(0, number.getText().toString().length()-1));
}*/
}
複製代碼
作者:
陳思惟
時間:
2018-10-6 11:41
package com.example.student.myapplication;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
import static android.Manifest.permission.*;
public class MainActivity extends AppCompatActivity {
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn10;
Button btn11;
Button call;
Button back;
TextView number;
Stack st=new Stack();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button tmp = (Button) findViewById(v.getId());
number.setText(number.getText().toString() + tmp.getText().toString());
st.push(tmp.getText().toString());
}
};
Button.OnLongClickListener a01 = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
};
private void Init() {
btn0 = (Button) findViewById(R.id.button0);
btn0.setOnClickListener(listener);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(listener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(listener);
btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(listener);
btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(listener);
btn5 = (Button) findViewById(R.id.button5);
btn5.setOnClickListener(listener);
btn6 = (Button) findViewById(R.id.button6);
btn6.setOnClickListener(listener);
btn7 = (Button) findViewById(R.id.button7);
btn7.setOnClickListener(listener);
btn8 = (Button) findViewById(R.id.button8);
btn8.setOnClickListener(listener);
btn9 = (Button) findViewById(R.id.button9);
btn9.setOnClickListener(listener);
btn10 = (Button) findViewById(R.id.button10);
btn10.setOnClickListener(listener);
btn11 = (Button) findViewById(R.id.button11);
btn11.setOnClickListener(listener);
call = (Button) findViewById(R.id.PhoneCall);
number = (TextView) findViewById(R.id.Number);
back=(Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!number.getText().toString().equals("")){
st.pop();
String out="";
for (Object str : st) {
out += str;
}
number.setText(out);
}
//number.setText(number.getText().toString().substring(0, number.getText().toString().length()-1));
}
});
back.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick(View v) {
number.setText("");
while(!st.empty())
st.pop();
return true;
}
});
}
public void PhoneCall(View view) {
try {
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:" + number.getText().toString()));
//startActivity(dial);
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(dial);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
}catch(Exception e)
{
number.setText(e.toString());
}
}
/*public void Back(View view) {
if(!number.getText().toString().equals(""))
number.setText(number.getText().toString().substring(0, number.getText().toString().length()-1));
}*/
}
複製代碼
作者:
黃璽安
時間:
2018-10-6 11:59
package com.example.student.myapplication;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
import static android.Manifest.permission.*;
public class MainActivity extends AppCompatActivity {
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn10;
Button btn11;
Button call;
Button BACK;
TextView number;
Stack<String>st=new Stack();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button tmp = (Button) findViewById(v.getId());
number.setText(number.getText().toString() + tmp.getText().toString());
st.push(tmp.getText().toString());
}
};
private void Init() {
btn0 = (Button) findViewById(R.id.button0);
btn0.setOnClickListener(listener);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(listener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(listener);
btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(listener);
btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(listener);
btn5 = (Button) findViewById(R.id.button5);
btn5.setOnClickListener(listener);
btn6 = (Button) findViewById(R.id.button6);
btn6.setOnClickListener(listener);
btn7 = (Button) findViewById(R.id.button7);
btn7.setOnClickListener(listener);
btn8 = (Button) findViewById(R.id.button8);
btn8.setOnClickListener(listener);
btn9 = (Button) findViewById(R.id.button9);
btn9.setOnClickListener(listener);
btn10 = (Button) findViewById(R.id.button10);
btn10.setOnClickListener(listener);
btn11 = (Button) findViewById(R.id.button11);
btn11.setOnClickListener(listener);
call = (Button) findViewById(R.id.PhoneCall);
number = (TextView) findViewById(R.id.Number);
BACK = (Button) findViewById(R.id.BACK);
BACK.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
number.setText("");
return false;
}
});
BACK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
st.pop();
String str = "";
for (String data : st) {
str += data;
}
number.setText(str);
}
});
}
public void Call(View view) {
try {
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:" + number.getText().toString()));
startActivity(dial);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
}catch(Exception e)
{
number.setText(e.toString());
}
}
}
package com.example.student.myapplication;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
import static android.Manifest.permission.*;
public class MainActivity extends AppCompatActivity {
Button btn0;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn10;
Button btn11;
Button call;
Button BACK;
TextView number;
Stack<String>st=new Stack();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button tmp = (Button) findViewById(v.getId());
number.setText(number.getText().toString() + tmp.getText().toString());
st.push(tmp.getText().toString());
}
};
private void Init() {
btn0 = (Button) findViewById(R.id.button0);
btn0.setOnClickListener(listener);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(listener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(listener);
btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(listener);
btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(listener);
btn5 = (Button) findViewById(R.id.button5);
btn5.setOnClickListener(listener);
btn6 = (Button) findViewById(R.id.button6);
btn6.setOnClickListener(listener);
btn7 = (Button) findViewById(R.id.button7);
btn7.setOnClickListener(listener);
btn8 = (Button) findViewById(R.id.button8);
btn8.setOnClickListener(listener);
btn9 = (Button) findViewById(R.id.button9);
btn9.setOnClickListener(listener);
btn10 = (Button) findViewById(R.id.button10);
btn10.setOnClickListener(listener);
btn11 = (Button) findViewById(R.id.button11);
btn11.setOnClickListener(listener);
call = (Button) findViewById(R.id.PhoneCall);
number = (TextView) findViewById(R.id.Number);
BACK = (Button) findViewById(R.id.BACK);
BACK.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
number.setText("");
return false;
}
});
BACK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
st.pop();
String str = "";
for (String data : st) {
str += data;
}
number.setText(str);
}
});
}
public void Call(View view) {
try {
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:" + number.getText().toString()));
startActivity(dial);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
}catch(Exception e)
{
number.setText(e.toString());
}
}
}
複製代碼
作者:
蔡季樺
時間:
2018-10-6 14:59
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn0;
Button btn11;
Button btn12;
Button cal;
Button back;
EditText et;
Stack<String> stack = new Stack();
private final static String CALL = "android.intent.action.CALL";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
Button tmp = findViewById(v.getId());
et.setText(et.getText().toString() + tmp.getText().toString());
stack.push(tmp.getText().toString());
}
};
public void init() {
btn1 = findViewById(R.id.button);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn4 = findViewById(R.id.button4);
btn5 = findViewById(R.id.button5);
btn6 = findViewById(R.id.button6);
btn7 = findViewById(R.id.button7);
btn8 = findViewById(R.id.button8);
btn9 = findViewById(R.id.button9);
btn0 = findViewById(R.id.button0);
btn11 = findViewById(R.id.button10);
btn12 = findViewById(R.id.button12);
cal = findViewById(R.id.call);
et = findViewById(R.id.editText);
back = findViewById(R.id.button15);
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
btn4.setOnClickListener(listener);
btn5.setOnClickListener(listener);
btn6.setOnClickListener(listener);
btn7.setOnClickListener(listener);
btn8.setOnClickListener(listener);
btn9.setOnClickListener(listener);
btn0.setOnClickListener(listener);
btn11.setOnClickListener(listener);
btn12.setOnClickListener(listener);
back.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
et.setText("");
return false;
}
});
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stack.pop();
String st ="";
for(String data:stack)
{
st += data;
}
et.setText(st);
}
});
cal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CALL_PHONE)) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("我真的沒有要做壞事, 給我權限吧?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
10);
Intent call = new Intent(CALL, Uri.parse("tel:" + et.getText()));
startActivity(call);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.CALL_PHONE},
10);
Intent call = new Intent(CALL, Uri.parse("tel:" + et.getText()));
startActivity(call);
}
}
});
}
}
複製代碼
作者:
洪振庭
時間:
2018-10-8 19:17
package com.example.student.myapplication;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static android.Manifest.permission.*;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,star,holl,dial,delete;
TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
Button.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button tmp = (Button) findViewById(v.getId());
output.setText(output.getText().toString() + tmp.getText().toString());
}
};
public void Init(){
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(listener);
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(listener);
b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(listener);
b4=(Button)findViewById(R.id.button4);
b4.setOnClickListener(listener);
b5=(Button)findViewById(R.id.button5);
b5.setOnClickListener(listener);
b6=(Button)findViewById(R.id.button6);
b6.setOnClickListener(listener);
b7=(Button)findViewById(R.id.button7);
b7.setOnClickListener(listener);
b8=(Button)findViewById(R.id.button8);
b8.setOnClickListener(listener);
b9=(Button)findViewById(R.id.button9);
b9.setOnClickListener(listener);
b0=(Button)findViewById(R.id.button0);
b0.setOnClickListener(listener);
star=(Button)findViewById(R.id.star);
star.setOnClickListener(listener);
holl=(Button)findViewById(R.id.holl);
holl.setOnClickListener(listener);
delete=(Button)findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!output.getText().toString().equals(""))
output.setText(output.getText().toString().substring(0, output.getText().toString().length()-1));
}
});
delete.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
output.setText("");
return true;
}
});
dial=(Button)findViewById(R.id.dial);
output=(TextView)findViewById(R.id.output);
}
public void PhoneCall(View view){
try {
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:" + output.getText().toString()));
startActivity(dial);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
}catch(Exception e)
{
output.setText(e.toString());
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2