安卓天天练练(三)常用组件Toast
要写几句java
package com.narumi.android_7_2; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1); btn.setOnClickListener( new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你已经点了我", 2000).show();
}
});
}
}
xml也很简单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.narumi.android_7_2.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="48dp"
android:layout_toRightOf="@id/textView1"
android:text="点我试试" /> </RelativeLayout>
要注意的的是java里面
btn.setOnClickListener( new OnClickListener() { 这样的写法可能会报错"Cannot be resolved as a type",说不能把new OnClickListener当成参数,
“The method setOnClickListener(View.OnClickListener)in the type View is not applicable for the arguments new OnClickListener(){} ”
那是为什么呢?
是因为它没找到这个对象的父类,如果我们写成new view.OnClickListener 就不会报错,
或者,用IDE的提示框中解决方法之一:import android.view.View 来实例化改对象,
即:"import OnClickListener (android.view.View)"也可以,点完这个快速修复的链接IDE自动import到头部,看不到view.前缀也是合法的了。
此练结束,点按钮弹出toast。
安卓天天练练(三)常用组件Toast的更多相关文章
- 安卓天天练练(十一)用list绑数据
之前在练练ListView的时候就有个 BaseAdapter ba = new BaseAdapter() { 用Adapter来绑数据的方法,该方法中 LinearLayout ll=new Li ...
- 安卓天天练练(十五)改造BasicSyncAdapter
谷歌的官方示例BasicSyncAdapter是Android Studio工程, 把它依样画葫芦到Eclipse上,然后改造成我需要的样式. 看官方示例源码的时候,看到EntryListActivi ...
- 安卓天天练练(十)ListView
ListView不能和ScrollView同时使用,因为它已经包含了滚动支持. 还有个Gallery http://blog.csdn.net/dazlly/article/details/78639 ...
- 安卓天天练练(五)CompoundButton
ToggleButton 让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的.. 注意: 如果LinearLayout,与RelativeLayout不同,必须有orientation ...
- 安卓天天练练(四)drawable state 属性
今天又作茧自缚(item 写成 itme ╮(╯▽╰)╭ elipse还自动闭合了标签,来回查查查看报错,为啥点击无效呢!) 真欠抽,怪不得上班地铁上被个sb踢到脚趾头(目测有可能是同家公司的..同站 ...
- Jmeter Web 性能测试入门 (三):Jmeter 常用组件说明
线程组:用来设置并发的数量和模式.是用来模拟用户并发的组件.JMeter 的每个任务都是用线程来处理的. 线程数:要并发的请求数量. Ramp-Up Period: 在多次时间内把这些并发的请求发送完 ...
- 第18讲- UI常用组件之EditText
第18讲UI常用组件之EditText 三.文本输入框EditText EditTex类继承自TextView.EditText是接受用户输入信息的最重要控件.在html当中,相当于<input ...
- 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)
什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...
- Android常用组件
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
随机推荐
- CCProgressTimer用法
bool HelloWorld::init(){ if ( !CCLayerColor::initWithColor(ccc4(255, 255, 2555, 255))){ return false ...
- 8 Pratical Examples of Linux “Touch” Command--reference
In Linux every single file is associated with timestamps, and every file stores the information of l ...
- request对象
Servlet三大域对象的应用 request.session.application(ServletContext) 请求转发器: public void doGet(HttpServletRequ ...
- JavaScript 是世界上最好的语言?
2016年1月中旬,Stack Overflow发起本年度的开发者调查,调查结果于近日公布.本文盘点 JS 开发者应该会关心的部分数据. Stack Overflow 技术排行榜: 在2015年6月, ...
- Linux下安装Python pip
在Python环境下,pip提供类似yum一样的下载方式,比easy_install方便的多. 1.下载get-pip.py wget https://bootstrap.pypa.io/get-pi ...
- java web 中的转发和重定向
假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...
- getSharedPreferences()与getSharedPreferences()与getDefaultSharedPreferences()的区别
http://blog.csdn.net/ah200614435/article/details/7869681 一直迷惑于这三个方法的关系,最近忙完项目,好好的分析一下. 如果你熟悉Context那 ...
- android系统体系结构
android系统底层是建立在Linux系统之上的,如下图 从上图可以看出android系统有五部分组成 1.APPLICATIONS(应用程序层) 包含一些核心应用程序,电子邮件,日历,地图,浏览器 ...
- oracle 消除块竞争(hot blocks)
上篇日志提到了,那么高的负载,是存在数据块读竞争,下面介绍几个方法来消除块竟争 查找块竟争 SELECT p1 "file#", p2 "block#", p3 ...
- sql server抓取表结构的语句
sql server 2008抓取方法: --------------------------------------- SELECT 表名 = Case When A.colorder ...