備忘錄_20160105(定位) 修改 回首頁

程式 2019-01-29 17:06:18 1548752778 100
onlcik 方法參考 (android, javascript)

onlcik 方法參考 (android, javascript)


●●●●activity_main.xml●●●●

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android=......>

  ....

  <Button
      android:id="@+id/btnOk"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:onClick="btnOk_onClick"
      android:text="@string/click_me" />

  ....

</android.support.constraint.ConstraintLayout>

●●●●MainActivity.java●●●●

  public class MainActivity extends AppCompatActivity 
  {
    ......
    public void btnOk_onClick(View v)
    {
        ....
    }
  }


●●●●activity_main.xml●●●●

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android=......>

  ....

  <Button
      android:id="@+id/btnOk"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="@string/click_me" />

  ....

</android.support.constraint.ConstraintLayout>

●●●●MainActivity.java●●●●

  public class MainActivity extends AppCompatActivity 
  {
    ......
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((Button)findViewById(R.id.btnOk)).setOnClickListener(btnOkOnClick);
    }
    ......
    
    private View.OnClickListener btnOkOnClick=new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            ......
        }
    };
    
  }

●●●●a.htm●●●●
<html>
  <head>
  </head>
  <body>
  
    <div onclick="sayHello();">
      Click Me! Using onclick!
    </div>
    
    <script>
      function sayHello()
      {
        alert("Hello, world! -- onclick");
      }
    </script>
    
    <hr />
    
    <div id="id02">
      Click Me! Using addEventListener!
    </div>
    
    <script>
      document.getElementById("id02").addEventListener(
        "click",
        function()
        {
          alert("Hello, world! -- addEventListener");
        }
      );
    </script>
  </body>
</html>