简体   繁体   English

单击按钮从Android中的EditText获取文本

[英]getting text from EditText in Android on click of button

Hi I am working on calculator in android. 嗨,我正在使用android中的计算器。 I am having a EditText in wchich no will displayed depending on click of particular no.for example when user press 1 it will display 1.now when user clicks on 2 it will display 12 and so on. 我正在根据某个特定编号的点击显示一个EditText,例如,当用户按下1时它将显示1.现在,当用户单击2时它将显示12,依此类推。

i have things like this 我有这样的事情

  public class Main extends Activity implements OnClickListener {

   public static int no1=0,no2=0,op=0,flag=0;
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    EditText display1=(EditText) findViewById(R.id.display);    


     Button one= (Button) findViewById(R.id.one);  
     one.setOnClickListener(this);
   }


public void onClick(View v) {

    if(v.getId()==R.id.one)
    {
        switch(flag)
        {
        case 1:no1*=10+1;display1.setText(no1);
        case 2:no2*=10+1;display1.setText(no2);
        }


    }

but here i am getting error as "display1 cannot be resolved". 但在这里我收到错误消息,因为“无法解析display1”。

any solution to make work..?? 任何解决方案,使工作.. ??

Because it's only available in your onCreate method. 因为它仅在您的onCreate方法中可用。 Declare it as a field in your class. 在班级中将其声明为字段。

private EditText display1;

Sometimes a complete code example helps: 有时,完整的代码示例有帮助:

public class Main extends Activity implements OnClickListener {

   private int no1=0;
   private int no2=0;
   private int op=0;
   private int flag=0;
   private EditText display1;

   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);


     display1=(EditText) findViewById(R.id.display);    


     Button one= (Button) findViewById(R.id.one);  
     one.setOnClickListener(this);
   }


    public void onClick(View v) {

      if(v.getId()==R.id.one)
      {
        switch(flag)
        {
        case 1:no1*=10+1;display1.setText(no1);
        case 2:no2*=10+1;display1.setText(no2);
        }


   }
}

Basically you want to declare display1 as a private instance variable. 基本上,您想将display1声明为私有实例变量。 The other variables do not need to be static, and should probably be declared private. 其他变量不必是静态的,应该声明为私有。 And declarations formatted on separate lines for improved readability. 声明在单独的行上格式化,以提高可读性。

Have fun. 玩得开心。

全局声明您的EditText和Button。

It should be better to keep a string showing in the EditText so that you have no difficulties appending and erasing. 最好在EditText中显示一个字符串,这样您就可以轻松地附加和擦除。 Then you can parse it to an integer so that you will be able to do math with it. 然后,您可以将其解析为整数,以便能够对其进行数学运算。

First you should declare your buttons and edit text globally: 首先,您应该声明按钮并全局编辑文本:

public class Main extends Activity implements OnClickListener {
     private EditText display1 = (EditText) findViewById(R.id.display);    
     private Button one= (Button) findViewById(R.id.one);
     private Button two= (Button) findViewById(R.id.two);    

If you have the button label set as the number you can use it like this: 如果您将按钮标签设置为数字,则可以这样使用它:

public void onClick(View view){
    display1.setText(display1.getText.toString() +  ((Button) view).getText().toString());
}    

That way, you can append the numbers to the end of the string in the display. 这样,您可以将数字附加到显示字符串的末尾。 If you want to use the number it's showing, you can use the following code: 如果要使用显示的数字,可以使用以下代码:

int number = Integer.parseInt(display1.getText().toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM