How to use Button and Listen for Click-Events.

  • We don’t want the OnScreenKeyBoard to appear
  •  when the cursor is on EditText so,
  •  let disable this.
  •  
    txtCTime.setInputType(InputType.TYPE_NULL);
    1</li>
        <li>Set the OnClickListener for the Button
    1
    b
  • Now do the logic inside onClick() to show the current time
    1
    txtCTime.setText(new Date().toString());
  • You need to import java package for using Date
    1
    import java.util.Date;
  • Finally, the default activity file should look like
     
    package com.suvendu.tutorials.button;
     
    import java.util.Date;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
     
    public class ButtonTutorialActivity extends Activity {
        Button btnCTime;
        EditText txtCTime;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btnCTime=(Button)findViewById(R.id.btnGenCurTime);
            txtCTime=(EditText)findViewById(R.id.txtShowCurTime);
            txtCTime.setInputType(InputType.TYPE_NULL);
            btnCTime.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View vw) {
                    txtCTime.setText(new Date().toString());
                }
            });
        }
    }

Average Rating (1)
by sharukh   multani  in Android  on 5/3/2015 12:20:23 AM  


Post Your Comment
*


nice
by   bhavesh   on  3/24/2015 4:14:04 AM