zoukankan      html  css  js  c++  java
  • Button

    初次接触Android,这个东西还是挺有意思的。界面的格式布局使用Xml文件进行设置,而所使用的代码则与布局分离,代之以Java文件,就象是写Asp.net文件。奇怪的是在代码中使用控件名称时,却类似于JavaScript一类的寻找,试了一下,如下图:

    代码
    package com.test.android;

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class HelloAndroid extends Activity {
        
    /** Called when the activity is first created. */
        @Override
        
    public void onCreate(Bundle savedInstanceState) {
            
    super.onCreate(savedInstanceState);
            
    //使用main的布局
            setContentView(R.layout.main);
            
    //获得Button对象
            Button btn = (Button)findViewById(R.id.Button01);
            
    //设置监听
            btn.setOnClickListener(new Button.OnClickListener(){
                
    public void onClick(View v){
                    dispToast(
    "点击了按钮");
                }
            });
            
    //获得TextView对象,此类如同.Net或Delphi中的Label
            TextView tv = (TextView)findViewById(R.id.TextView01);
            tv.setTextColor(Color.RED);
            tv.setTextSize(
    20);
            tv.setBackgroundColor(Color.YELLOW);
            tv.setText(
    "显示文本");
        }
        
    public void dispToast(String str){
            Toast.makeText(
    this, str, Toast.LENGTH_LONG).show();
        }
        
    //显示触点的位置
        public boolean onTouchEvent(MotionEvent event){
            
    int selectAction = event.getAction();
            
    if (selectAction == MotionEvent.ACTION_CANCEL ||
                selectAction 
    == MotionEvent.ACTION_DOWN ||
                selectAction 
    == MotionEvent.ACTION_MOVE)
                
    return false;
            
            
    int x = (int)event.getX();
            
    int y = (int)event.getY();
            
    this.dispToast("触点坐标: x " + Integer.toString(x)+" y "+Integer.toString(y));
            
    return super.onTouchEvent(event);
        }
    }
  • 相关阅读:
    java jdk1.8 32/64位 官方绿色版下载附安装教程
    坡度常用的表示方法
    就此道别
    阿里巴巴矢量图标库(iconfont)批量全选的方法
    thinkphp6.0 集成Alipay 手机和电脑端支付的方法
    法定的属于我的第23个年头已经结束,在今天迎来第24年的第一天。
    世界地图展开图,来自 Simon's World Map
    thinkphp6.0 composer 安装 web-token/jwt-framework 常见出错原因分析及解决方法
    thinkphp6 常用方法文档
    Python获取列表中的最后一个或者倒数第几个的方案
  • 原文地址:https://www.cnblogs.com/wjhx/p/1679439.html
Copyright © 2011-2022 走看看