Android中常用控件及属性
在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考。好了废话不多讲,现在开始我们本篇内容的介绍。
1、控制应用显示的方向:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖直显示效果。
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横向显示效果。
将上句添加在setContentView(R.layout.activity_one);下面即可。
2、文本单行显示及文本单行输入:
<!-- 设置文本显示在一行 -->
android:singleLine="true";
3、TabActivity的实现方法:
a、布局文件:
<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=".Activitytwo" > <TabHost
android:id="@+id/bookTabHost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/doneBook"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="边城"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="围城"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="追风筝的人"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout> <LinearLayout
android:id="@+id/doingBook"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="倾城之恋"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="灿烂千阳"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="活着"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout> <LinearLayout
android:id="@+id/willBook"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:text="百年孤独"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="房子里的大象"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="忏悔"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TabHost> </RelativeLayout>
b、activity代码:
public class Activitytwo extends TabActivity{
public TabHost bookth = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bookth = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_two, bookth.getTabContentView(), true);
bookth.addTab(bookth.newTabSpec("done").setIndicator("已读").setContent(R.id.doneBook));
bookth.addTab(bookth.newTabSpec("doing").setIndicator("正读").setContent(R.id.doingBook));
bookth.addTab(bookth.newTabSpec("will").setIndicator("未读").setContent(R.id.willBook));
}
}
c、效果图:
值得提醒的是,目前更为推崇的方式是ViewPager+fragement
4、文本输入提示:
a、布局文件代码:
<AutoCompleteTextView
android:id="@+id/acTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名:"
android:textColor="#000"
android:maxLength="10"
/>
<MultiAutoCompleteTextView
android:id="@+id/macTextView"
android:layout_below="@id/acTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入城市:"
android:textColor="#000"
android:maxLength="5"
/>
b、Activity代码:
public class Activityfive extends Activity{ private AutoCompleteTextView acTextView;
private MultiAutoCompleteTextView macTextView;
private String [] arr = {"abc","abx","abo","bdc","bdf"};
private String [] brr = {"ab北京","ab南京","ab东京","bb莫斯科","bb英国","bb美国"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_five);
acTextView = (AutoCompleteTextView) findViewById(R.id.acTextView);
macTextView = (MultiAutoCompleteTextView) findViewById(R.id.macTextView);
ArrayAdapter<String> arrAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, arr);
acTextView.setAdapter(arrAdapt);
ArrayAdapter<String> brrAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, brr);
macTextView.setAdapter(brrAdapt);
macTextView.setThreshold(1);//设置输入多少个字符开始自动匹配
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());//设置分隔符
} }
5、发送广播:
a、创建广播内容:
public class Activityone extends Activity {
final String Intent_Action = "com.android.BroadcastReceiverDemo";//广播的地址;自定义设置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Intent intent = new Intent(Intent_Action);
intent.putExtra("name", "小米");//广播的内容
Activityone.this.sendBroadcast(intent);//发送广播
}
}
b、在AndroidManifest.xml中声明广播:
<!-- 设置广播接收器 -->
<receiver
android:name="cn.edu.hpu.android.activity_broadcast.MyBroadcastReceiver"
android:enabled="true"
>
<intent-filter>
<action android:name="com.android.BroadcastReceiverDemo" />
</intent-filter>
</receiver>
c、接收广播:
public class MyBroadcastReceiver extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) { String name = intent.getStringExtra("name");//接收广播发送出来的数据
Toast.makeText(context, " "+name, Toast.LENGTH_SHORT).show(); } }
6、获得用户网络状况:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得网络连接对象
ConnectivityManager nw = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = nw.getActiveNetworkInfo(); Toast.makeText(MainActivity.this, "当前网络"+add(netinfo.isAvailable())+","+"网络"+app(netinfo.isConnected())+","+"网络连接"+adp(netinfo.isConnected()), Toast.LENGTH_LONG).show();
} String add(Boolean bl){
String s = "不可用";
if(bl==true){
s="可用";
}
return s;
} String app(Boolean bl){
String s = "未连接";
if(bl==true){
s="已连接";
}
return s;
} String adp(Boolean bl){
String s = "不存在!";
if(bl==true){
s="存在!";
}
return s;
} }
注意:在AndroidManifest.xml文件中添加获取网络的权限:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
7、SharedPreferences存储:
SharedPreferences sp = getSharedPreferences(FILE_NAME, Context.MODE_APPEND);
Editor editor = sp.edit();//获得编辑对象
editor.clear();
editor.putString("name", name);
editor.putString("password", number);
editor.commit();//提交内容保存
注意:FILE_NAME是我们设置的唯一标识,方便我们进行查询和修改。
8、SharedPreferences内容获得:
SharedPreferences sp = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
String name1 = sp.getString("name", "");
String password = sp.getString("password", "");
注意:FILE_NAME:要和我们上面设置的一致,这样才能取得上面的我们保存的内容。sp.getString("name", "");简单介绍一下,第一个参数是我们需要获得的数据存储时的标签,第二个参数是当我们获得数据不存在时,返回的内容。
9、Toast使用:
a、系统自带形式:
Toast.makeText(getApplicationContext(), "系统自带提示形式", Toast.LENGTH_SHORT).show();
b、自定义形式:
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, , );//设置显示的位置
toast.setDuration(Toast.LENGTH_LONG);//设置显示的时间 toast.setView(getLayoutInflater().inflate(R.layout.activity_two, null));//设置自定义的视图 toast.show();
效果图:
Android中常用控件及属性的更多相关文章
- WPF中常用控件的属性
Source = new BitmapImage( new Uri( WangCaiConfig.GetCurrentDirectory() + imgStr, UriKind.RelativeOrA ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- Android笔记---常用控件以及用法
这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...
- Android中ExpandableListView控件基本使用
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- android中ListView控件&&onItemClick事件中获取listView传递的数据
http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...
- android中RecyclerView控件实现点击事件
RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...
- android中RecyclerView控件实现瀑布流布局
本文是在之前文章的基础上做的修改:android中RecyclerView控件的使用 1.修改列表项news_item.xml: <?xml version="1.0" en ...
- android中RecyclerView控件的列表项横向排列
本文是在上一篇文章的基础上做的修改:android中RecyclerView控件的使用 1.修改列表项news_item.xml:我这里是把新闻标题挪到了新闻图片的下面显示 <?xml vers ...
随机推荐
- NetBIOS与Winsock编程接口
最近在看网络编程方面的书,由于不是通信专业出身的,以前理解的网络体系感觉就是tcp/ip,最近工作上接触到了一些光环网等乱七八糟的东西,有些基本的LC.SC连接器都不认识.花时间看了下计算机网络体系结 ...
- Hbase随笔2
Hbase是建立在HDFS上的分布式数据库,下图是Hbase表的模型: Hbase这个数据库其实和传统关系数据库还是有很多类似之处,而不是像mongodb,memcached以及redis完全脱离了表 ...
- Cookie与Session的区别-总结很好的文章
Cookie与Session的区别-总结很好的文章 本文分别对Cookie与Session做一个介绍和总结,并分别对两个知识点进行对比分析,让大家对Cookie和Session有一个更深入的了解,并对 ...
- wp学习计划!
对wp兴趣很大,可真正学习时,目的性就很不明确了,其实这有两大问题阻碍着,一是自身的基础还不够扎实,二是学习资料不好找,前者只能慢慢积累,而后者却是可以改变的,现在我的主阵营是channel 9上的w ...
- JavaScript this用法总结
在JavaScript中,this关键字可以说是最复杂的机制之一.对this的作用机制缺乏比较深入的理解很容易在实际开发中出现问题. 1.this的作用 为什么要在JavaScript中使用this呢 ...
- [转]全面理解Unity加载和内存管理
[转]全面理解Unity加载和内存管理 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质 ...
- Task Runner Explorer for vs2015找不到啊
https://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708/view/ 编译typescri ...
- C#函数式编程之可选值
在我们的实际开发中已经会遇到可空类型,而在C#中自从2.0之后就提供了可空类型(Nullable<T>),普通的值类型是不可以赋值为NULL,但是在类型的后面加上问号就变成了可空类型,这样 ...
- 【转】 Nginx深入详解之多进程网络模型
[转自]http://blog.chinaunix.net/uid-22312037-id-3974068.html 一.进程模型 Nginx之所以为广大码农喜爱,除了其高性能外,还有其 ...
- 人人都是 DBA(VII)B 树和 B+ 树
B 树(B-Tree)是为磁盘等辅助存取设备设计的一种平衡查找树,它实现了以 O(log n) 时间复杂度执行查找.顺序读取.插入和删除操作.由于 B 树和 B 树的变种在降低磁盘 I/O 操作次数方 ...