Android学习:onClick +findViewById+Toast基础

来源:互联网 发布:什么是数据挖掘 编辑:程序博客网 时间:2024/04/19 22:47

一:源码







二:知识点

1:自定义button时间Toast
(1)在activity_main.xml中,给控件button加上click事件
android:onClick="test"
其中test是自定义函数名称
(2)在MainActivity.java中声明test函数
(3)test函数中加Toast消息提醒
Toast toast = Toast.makeText(MainActivity.this, "点击了Button",Toast.LENGTH_SHORT);
//屏幕居中显示,X轴和Y轴偏移量都是0  
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();


2:findViewById
获取元素,通过R类
R.id.textView1
R.string.textChange



3:自定义String元素
在res/values/strings.xml中
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="buttonMsg">clickMe</string>
</resources>



4:国际化 
在res下面创建目录values-zh-rCN
创建strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="buttonMsg">点击我</string>
</resources>

0 0