Get Value of a Edit Text field

前端 未结 12 1098
既然无缘
既然无缘 2020-11-22 07:20

I am learning how to create UI elements. I have created a few EditText input fields. On the click of a Button I want to capture the content typed into that input field.

12条回答
  •  星月不相逢
    2020-11-22 08:08

    step 1 : create layout with name activity_main.xml

    
        
        
        

    Step 2 : Create class Main.class

    public class Main extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = (Button) findViewById(R.id.btn);
            final TextView tv = (TextView) findViewById(R.id.tv);
            final EditText et = (EditText) findViewById(R.id.et);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    String country = et.getText().toString();
                    tv.setText("Your inputted country is : " + country);
                }
            });
     }
    }
    

提交回复
热议问题