Android 编辑文本的按钮值?
我有一个EditText和像电话号码这样的按钮,我把每个按钮都放在EditText上,以便在EditText上设置Text,但为什么不在彼此旁边写呢。。? 当我按1然后按2时,整个编辑文本仅为2,如果我单击3,则2将被删除,3将被删除,我希望像键盘一样,文本彼此相邻Android 编辑文本的按钮值?,android,button,android-edittext,Android,Button,Android Edittext,我有一个EditText和像电话号码这样的按钮,我把每个按钮都放在EditText上,以便在EditText上设置Text,但为什么不在彼此旁边写呢。。? 当我按1然后按2时,整个编辑文本仅为2,如果我单击3,则2将被删除,3将被删除,我希望像键盘一样,文本彼此相邻 public class MainActivity extends Activity implements View.OnClickListener { EditText phoneTo; Button btn,b
public class MainActivity extends Activity implements View.OnClickListener {
EditText phoneTo;
Button btn,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn99;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneTo = (EditText) findViewById(R.id.editText2);
btn = (Button)findViewById(R.id.button);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
btn3 = (Button)findViewById(R.id.btn3);
btn4 = (Button)findViewById(R.id.btn4);
btn5 = (Button)findViewById(R.id.btn5);
btn6 = (Button)findViewById(R.id.btn6);
btn7 = (Button)findViewById(R.id.btn7);
btn8 = (Button)findViewById(R.id.btn8);
btn9 = (Button)findViewById(R.id.btn9);
btn10 = (Button)findViewById(R.id.btn10);
btn11 = (Button)findViewById(R.id.btn11);
btn12 = (Button)findViewById(R.id.btn12);
btn99 = (Button)findViewById(R.id.btn99);
phoneTo.setSelection(phoneTo.getText().length());
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makePhoneCall(view);
}
});
}
public void makePhoneCall(View view) {
try {
String number = phoneTo.getText().toString();
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(phoneIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"Call failed, please try again later!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
phoneTo.setText("1");
break;
case R.id.btn2:
phoneTo.setText("2");
break;
case R.id.btn3:
phoneTo.setText("3");
break;
case R.id.btn4:
phoneTo.setText("4");
break;
case R.id.btn5:
phoneTo.setText("5");
break;
case R.id.btn6:
phoneTo.setText("6");
break;
case R.id.btn7:
phoneTo.setText("7");
break;
case R.id.btn8:
phoneTo.setText("8");
break;
case R.id.btn9:
phoneTo.setText("9");
break;
case R.id.btn10:
phoneTo.setText("0");
break;
case R.id.btn11:
phoneTo.setText("*");
break;
case R.id.btn12:
phoneTo.setText("#");
break;
case R.id.btn99:
String str=phoneTo.getText().toString();
if (str.length() >1 ) {
str = str.substring(0, str.length() - 1);
phoneTo.setText(str);
}
else if (str.length() <=1 ) {
phoneTo.setText("");
}
break;
}
}
}
public类MainActivity扩展活动实现View.OnClickListener{
编辑文本电话;
按钮btn、btn1、btn2、btn3、btn4、btn5、btn6、btn7、btn8、btn9、btn10、btn11、btn12、btn99;
文本视图文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneTo=(EditText)findViewById(R.id.editText2);
btn=(按钮)findViewById(R.id.Button);
btn1=(按钮)findViewById(R.id.btn1);
btn2=(按钮)findViewById(R.id.btn2);
btn3=(按钮)findViewById(R.id.btn3);
btn4=(按钮)findViewById(R.id.btn4);
btn5=(按钮)findViewById(R.id.btn5);
btn6=(按钮)findViewById(R.id.btn6);
btn7=(按钮)findViewById(R.id.btn7);
btn8=(按钮)findViewById(R.id.btn8);
btn9=(按钮)findViewById(R.id.btn9);
btn10=(按钮)findViewById(R.id.btn10);
btn11=(按钮)findViewById(R.id.btn11);
btn12=(按钮)findViewById(R.id.btn12);
btn99=(按钮)findViewById(R.id.btn99);
phoneTo.setSelection(phoneTo.getText().length());
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
makePhoneCall(视图);
}
});
}
公共void makePhoneCall(视图){
试一试{
字符串编号=phoneTo.getText().toString();
Intent phoneIntent=新意图(Intent.ACTION\u调用);
setData(Uri.parse(“电话:”+number));
if(ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL\u PHONE)!=PackageManager.permission\u已授予){
返回;
}
起始触觉(电话意向);
}捕获(android.content.ActivityNotFoundException ex){
Toast.makeText(MainActivity.this,
“呼叫失败,请稍后再试!”,Toast.LENGTH\u SHORT.show();
}
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.btn1:
phoneTo.setText(“1”);
打破
案例R.id.btn2:
phoneTo.setText(“2”);
打破
案例R.id.btn3:
phoneTo.setText(“3”);
打破
案例R.id.btn4:
phoneTo.setText(“4”);
打破
案例R.id.btn5:
phoneTo.setText(“5”);
打破
案例R.id.btn6:
phoneTo.setText(“6”);
打破
案例R.id.btn7:
phoneTo.setText(“7”);
打破
案例R.id.btn8:
phoneTo.setText(“8”);
打破
案例R.id.btn9:
phoneTo.setText(“9”);
打破
案例R.id.btn10:
phoneTo.setText(“0”);
打破
案例R.id.btn11:
phoneTo.setText(“*”);
打破
案例R.id.btn12:
phoneTo.setText(“#”);
打破
案例R.id.btn99:
String str=phoneTo.getText().toString();
如果(str.length()>1){
str=str.substring(0,str.length()-1);
phoneTo.setText(str);
}
否则,如果(str.length()您的switch语句错误,如果我得到了您的要求
你想让用户写一个完整的数字,但你每次都在使用这个数字
phoneTo.setText(number);
这意味着每次edittext都将用数字
值重置
改用phoneTo.append(mynumber);
希望对您有所帮助您必须附加字符串
像这样试试
case R.id.btn1:
String str=phoneTo.getText().toString()+"1";
phoneTo.setText(str);
break;
类似地,在所有情况下使用SetText()更改EditText中的整个文本时,必须将当前文本与输入关联起来:
phoneTo.setText(phoneTo.getText() + "1");
或者看看函数getText().append()、getText().insert()、getText().replace()您应该这样做,因为您这样做只是每次都用数字替换EditText
`
@覆盖
公共void onClick(视图v){
开关(v.getId()){
案例R.id.btn1:
String number=phoneTo.getText.toString();
数字+=“1”;
phoneTo.setText(数字);
打破
...
案例R.id.btn99:
String str=phoneTo.getText().toString();
如果(str.length()>1){
str=str.substring(0,str.length()-1);
phoneTo.setText(str);
}
否则,如果(str.length(),则需要在字符串中附加这些类型化字符,而不是将其放在edittext中directly@Dr.Easy欢迎:)还有一个问题:如果你没有使用View视图
方法,为什么你要将它传递给makePhoneCall
方法?我不想做inputtype=phone,所以当我按下自定义按钮时,我需要显示在编辑文本上,然后用call按钮拨打电话,不管怎样,谢谢。!
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
String number = phoneTo.getText.toString();
number += "1";
phoneTo.setText(number);
break;
...
case R.id.btn99:
String str=phoneTo.getText().toString();
if (str.length() >1 ) {
str = str.substring(0, str.length() - 1);
phoneTo.setText(str);
}
else if (str.length() <=1 ) {
phoneTo.setText("");
}
break;
}