首页 文章 精选 留言 我的

精选列表

搜索[游戏],共10000篇文章
优秀的个人博客,低调大师

【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiob...

3.4 EditText EditText类官方文档地址:http://developer.android.com/reference/android/widget/EditText.html EditText继承TextView,所以EditText具有TextView的属性特点,下面主要介绍一些EditText的特有的输入法的属性特点 android:layout_gravity="center_vertical":设置控件显示的位置:默认top,这里居中显示,还有bottom android:hin:Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。 android:singleLine:设置单行输入,一旦设置为true,则文字不会自动换行。 android:gray="top" :多行中指针在第一行第一位置et.setSelection(et.length());:调整光标到最后一行 android:autoText :自动拼写帮助。这里单独设置是没有效果的,可能需要其他输入法辅助才行 android:capitalize :设置英文字母大写类型。设置如下值:sentences仅第一个字母大写;words每一个单词首字母大小,用空格区分单词;characters每一个英文字母都大写。 android:digits :设置允许输入哪些字符。如“1234567890.+-*/%\n()” android:singleLine :是否单行或者多行,回车是离开文本框还是文本框增加新行android:numeric :如果被设置,该TextView接收数字输入。有如下值设置:integer正整数、signed带符号整数、decimal带小数点浮点数。 android:inputType:设置文本的类型 android:password :密码,以小点”.”显示文本 android:phoneNumber :设置为电话号码的输入方式。 android:editable :设置是否可编辑。仍然可以获取光标,但是无法输入。 android:autoLink=”all” :设置文本超链接样式当点击网址时,跳向该网址 android:textColor = "#ff8c00":字体颜色 android:textStyle="bold":字体,bold, italic, bolditalic android:textAlign="center":EditText没有这个属性,但TextView有 android:textColorHighlight="#cccccc":被选中文字的底色,默认为蓝色 android:textColorHint="#ffff00":设置提示信息文字的颜色,默认为灰色 android:textScaleX="1.5":控制字与字之间的间距 android:typeface="monospace":字型,normal, sans, serif, monospace android:background="@null":空间背景,这里没有,指透明 android:layout_weight="1":权重在控制控件显示的大小时蛮有用的。 android:textAppearance="?android:attr/textAppearanceLargeInverse":文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/txt_hint" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_GetEditTextStr" /> </LinearLayout> public class MainActivity extends Activity { private EditText et; // 创建一个文本编辑器对象 private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 获取EditText中的文本内容 String str = et.getText().toString(); // 让TextView将获取到的EditText内容str显示出来 tv.setText(str); } }); } } 3.5 CheckBox CheckBox类官方文档地址:http://developer.android.com/reference/android/widget/CheckBox.html <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <CheckBox android:id="@+id/cb1" android:layout_width="match_parent" android:layout_height="wrap_content" android:button="@drawable/checkboxstyle" android:text="@string/checkbox1" /> <CheckBox android:id="@+id/cb2" android:layout_width="match_parent" android:layout_height="wrap_content" android:button="@drawable/checkboxstyle" android:text="@string/checkbox2" /> <CheckBox android:id="@+id/cb3" android:layout_width="match_parent" android:layout_height="wrap_content" android:button="@drawable/checkboxstyle" android:text="@string/checkbox3" /> </LinearLayout> import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends Activity implements OnCheckedChangeListener { private CheckBox cb1, cb2, cb3; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 实例化3个CheckBox cb1 = (CheckBox) findViewById(R.id.cb1); cb2 = (CheckBox) findViewById(R.id.cb2); cb3 = (CheckBox) findViewById(R.id.cb3); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); } // 重写监听器的抽象函数 /* * onCheckedChanged(CompoundButton buttonView, boolean isChecked) * 第一个参数确定哪一个CheckBox状态发生改变 第二个参数确定改变的CheckBox的具体状态值,true为勾选,false为未勾选。 */ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // buttonView 选中状态发生改变的那个按钮 // isChecked 表示按钮新的状态(true/false) if (cb1 == buttonView || cb2 == buttonView || cb3 == buttonView) { if (isChecked) { // 显示一个提示消息 toastDisplay(buttonView.getText() + "选中"); } else { toastDisplay(buttonView.getText() + "取消选中"); } } } private void toastDisplay(String str) { /* * Toast.makeText(Context context, CharSequence text,int duration); * 第一个参数是上下文对象 第二个参数是显示的文本内容 * 第三个参数是显示提示消息的持续时间;其值有2个参数LENGTH_SHORT(短暂持续)和LENGTH_LONG(略长持续): * 最后,使用Toast对象调用show()方法即可 */ Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } } CheckBox样式修改: 在res/drawable中添加checkbox自定义样式的xml文件。 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 选中状态样式 --> <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"></item> <!-- 未选中状态样式 --> <item android:drawable="@drawable/checkbox_normol" android:state_checked="false"></item> </selector> 然后在布局文件中引用就可以了。 <CheckBox android:id="@+id/cb1" android:layout_width="match_parent" android:layout_height="wrap_content" android:button="@drawable/checkboxstyle" android:text="@string/checkbox1" /> 3.6 Radiobutton Radiobutton类官方文档地址:http://developer.android.com/reference/android/widget/RadioButton.html RadioButton与CheckBox监听步骤类似,但RadioButton监听需要注意三点: 1.RadioButton与CheckBox使用的监听器不同。 2.RadioButton绑定监听的时候,不是每个RadioButton都去绑定,因为所有的RadioButton都被放在了RadioGroup单选组中,所以只需要将RadioGroup绑定上监听器即可。 3.重写监听器函数onCheckedChanged(RadioGroup group, int checkedId),这个函数的第一个参数是单选组,注意第二个参数,这里的checkedId不是RadioGroup单选组中的每个RadioButton的下标,而是发生状态改变的RadioButton的内存ID。所以在进行判断哪个RadioButton发生状态改变的时候,可以利用RadioButton.getID来与checkedId进行对比 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/radGrp" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/rb1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/rb1" /> <RadioButton android:id="@+id/rb2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/rb2" /> <RadioButton android:id="@+id/rb3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/rb3" /> </RadioGroup> </LinearLayout> import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends Activity implements OnCheckedChangeListener { private RadioButton rb1, rb2, rb3; private RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rb1 = (RadioButton) findViewById(R.id.rb1); rb2 = (RadioButton) findViewById(R.id.rb2); rb3 = (RadioButton) findViewById(R.id.rb3); rg = (RadioGroup) findViewById(R.id.radGrp); rg.setOnCheckedChangeListener(this);// 将单选组绑定监听器 } // 重写监听器函数 /* * onCheckedChanged(RadioGroup group, int checkedId) 第一个参数是单选组 * 第二个参数这里的checkedId发生改变的RadioButton的内存ID。 * 在进行判断哪个RadioButton发生状态改变的时候,可以利用RadioButton.getID来与checkedId进行对比 */ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (group == rg) { String rbName = null; if (checkedId == rb1.getId()) { rbName = rb1.getText().toString(); } else if (checkedId == rb2.getId()) { rbName = rb2.getText().toString(); } else if (checkedId == rb3.getId()) { rbName = rb3.getText().toString(); } Toast.makeText(this, "选择了下标为“" + rbName + "”的单选按钮", Toast.LENGTH_SHORT).show(); } } } 本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/3737949.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

【读书笔记《Android游戏编程之从零开始》】3.Android 游戏开发常用的系统控件(Button、Layout、ImageButto...

3.1 Button Button这控件不用多说,就是一个按钮,主要是点击后进行相应事件的响应。 给组件添加ID属性:定义格式为 android:id="@+id/name",这里的name是自定义的,不是索引变量。“@+”表示新声明,"@"表示引用,例如: "@+id/tv" 表示新声明一个id,是id名为tv的组件; "@id/tv" 表示引用id名为tv的组件。 给按钮添加点击事件响应 想知道按钮是否被用户点击,就需要一个“点击监听器”来对其进行监听,然后通过监听器来获取点击的事件,就可以判断关注的按钮是否被用户所点击。 使用监听器有两种方式:1.当前类使用点击监听器接口,修改后源码如下: //使用点击监听器 public class ButtonProject extends Activity implements OnClickListener { private Button btn_submit,btn_cancel; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.button_project_layout); btn_submit = (Button) findViewById(R.id.btn_submit); btn_cancel = (Button)findViewById(R.id.btn_cancel); tv = (TextView)findViewById(R.id.tv); btn_submit.setOnClickListener(this); btn_cancel.setOnClickListener(this); } //使用点击监听器必须重写其抽象函数, //@Override 表示重写函数 @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == btn_submit) { tv.setText("确定按钮触发事件!"); }else if(v==btn_cancel) { tv.setText("取消按钮触发事件!"); } } } 先用当前类使用点击监听器接口(onClickListner),重写点击监听器的抽象函数(onClick);然后对需要监听的按钮进行按钮绑定监听器操作,这样监听器才能对绑定的按钮进行监听,以判断其是否被用户点击,一旦按钮被点击,就会自动响应onClick函数,并将点击的button(button也是 1个view)传入;最后就可以在onClick函数中书写点击会触发的事件(因为定义了多个按钮,所以在onClick函数中对系统传入的view进行按钮匹配的判断,让不同的按钮做不通的处理事件)。 2.使用内部类实现监听器进行监听,修改后源代码如下: public class ButtonProject extends Activity { private Button btn_submit,btn_cancel; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.button_project_layout); btn_submit = (Button) findViewById(R.id.btn_submit); btn_cancel = (Button)findViewById(R.id.btn_cancel); tv = (TextView)findViewById(R.id.tv); //将btn_submit按钮绑定点击监听器 btn_submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub tv.setText("确定按钮触发事件!"); } }); btn_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText("取消按钮触发事件!"); } }); } } 利用内部类的形式也需要重写监听器的抽象函数,然后在onClick里进行处理事件,这里不用判断view了,因为一个Button对应了一个监听器。 Button类官方文档地址:http://developer.android.com/reference/android/widget/Button.html 其中有说明,如果不用OnClickListener监听器的话,现在可以在XML布局按钮控件中使用android:onClick属性。然后在类中调用你设置的onClick方法。代码如下: <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/self_destruct" android:onClick="selfDestruct" /> 当用户单击按钮时,Android系统调用selfDestruct方法。该方法必须是公开并接受一个视图作为其唯一的参数。 public void selfDestruct(View view) { // 按钮响应事件 } 3.2 Layout 1.线性布局 LinearLayout LinearLayout类官方文档地址:http://developer.android.com/reference/android/widget/LinearLayout.html LinearLayout(线性布局)是5中布局中最常用的一种,此布局在显示组件的时候会默认保持组件的间隔以及组件之间的相互对其(相对一个组件的右对齐、中间对齐或者左对齐)。线性布局显示组件的方式有垂直与水平两种,可以通过orienrarion进行设定。 orientation属性表示设置布局中的控件的方向,其属性值有两种,一种是“vertical”垂直排列(见下图左),另一种是“horizontal”水平排列(见下图右)。 (1)gravity:每个组件默认其值为左上角对齐,其属性可以调整组件对齐方式,比如向左、向右、或者居中对齐等。 (2)padding:边距的填充,也称内边距。其边距属性有: android:paddingTop,设置上边距; android:paddingBottom,设置下边距; android:paddingLeft,设置左边距; android:paddingRight,设置右边距; android:padding则表示周围四方向各内边距统一调整。 边距属性值为具体数字。 (3)layout_margn:外边距,其上下左右边距属性类似内边距。 padding内边距指的是当前布局与包含的组件之间的边距;layout_margn外边距指的是与其他组件之间的边距。 2.相对布局 RelativeLayout RelativeLayout类官方文档地址:http://developer.android.com/reference/android/widget/RelativeLayout.html RelativeLayout(相对布局):除了最常用的LinearLayout之外,相对布局则是另一种常用的布局。与线性布局不同之处在于,线性布局如果需要将一组件对齐另外一个组件就必须将所有的组件进行对齐,或者使用嵌套布局才可以完成;但是相对布局不必这么麻烦。因为在相对布局中,每个组件都可以指定相对于其他组件或父组件的位置,只是必须通过ID来进行指定。修改main.xml布局如下; 所以在相对布局中,如果想固定一个组件的位置,至少要确定组件“左右”与“上下”两个位置才可以准确固定组件的位置。 3.表格布局 TableLayout TableLayout类官方文档地址:http://developer.android.com/reference/android/widget/TableLayout.html Tablelayout(表格布局)的样式如同一个表格。通常情况下,TableLayout有多个TableRow组成,每个TableRow就是一行,定义几个TableRow就是定义几行:TableLayout不会显示行列号,也没有分割线。其行数和列数也是有自己来操作和确定的。 下面来介绍在TableLayout中常用的几个属性: (1)shrinkColumns属性:以0为序,当TableRow里面的控件布满布局时,指定列自动延伸以填充可用部分;当TableRow里面的控件还没有布满布局时,不起作用。 (2)strechColumns属性:以第0行为序,指定列对空白部分进行填充。 (3)collapseColumns属性:以第0行为序,隐藏指定的列。 (4)layout_column属性:以第0行为序,设置组件显示在指定列。 (5)layout_span属性:以第0行为序,设置组件显示占用的列数。 4.绝对布局 AbsoluteLayout AbsoluteLayout类官方文档地址:http://developer.android.com/reference/android/widget/AbsoluteLayout.html AbsoluteLayout(绝对布局)布局用法如其名,组件的位置可以准确的指定其在屏幕的x/y坐标位置。虽然可以精确的去规定坐标,但是由于代码的书写过于刚硬,使在不同的设备,不同分辨率的手机移动设备上不能很好的显示应有的效果,此布局设定组件位置都是用x/y来进行调整,又使得整体布局格式代码很不灵活,所以此布局不推荐使用。 5.单帧布局 FrameLayout FrameLayout类官方文档地址:http://developer.android.com/reference/android/widget/FrameLayout.htm FrameLayout(单帧布局)是5中布局中最简单的一种,因为单帧布局在新定义组件的时候永远都会将组件放在屏幕的左上角,即使在此布局中定义多个组件,后一个组件总会将前一个组件覆盖,除非最后一个组件是透明的。 3.3 ImageButton ImageButton类官方文档地址:http://developer.android.com/reference/android/widget/ImageButton.html ImageButton与Button类似 ,区别在于ImageButton可以自定义一张图片作为一个按钮;也正因为使用图片代替了按钮,所以ImageButton按下与抬起的样式效果需要自定义。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageButton android:id="@+id/btn_ImageButton" android:layout_marginLeft="100dp" android:layout_marginTop="100dp" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/dsfvsdf" /> </LinearLayout> public class ImageButtonProject extends Activity { private ImageButton btn_ImageButton; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.imagebutton_layout); btn_ImageButton = (ImageButton)findViewById(R.id.btn_ImageButton); //为图片按钮添加触屏监听 btn_ImageButton.setOnTouchListener(new OnTouchListener() { @Override //第一个参数:表示触发触屏事件的事件源view //第二个参数:表示触屏事件的类型,如按下、抬起、移动等。 public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction()==MotionEvent.ACTION_DOWN)//按下事件 { //设置图片按钮背景 //getResources().getDrawable(int ID) 传入图片ID得到一个Drawable对象。 btn_ImageButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.dsfvsdf_bg)); }else if(event.getAction()==MotionEvent.ACTION_UP)//抬起事件 { btn_ImageButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.dsfvsdf)); } return false; } }); } } 显示不同的按钮状态官方文档中有提到可以在单独在xml文件中配置,然后布局文件中引用就可以了,代码如下: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed 按下状态--> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused 聚焦状态--> <item android:drawable="@drawable/button_normal" /> <!-- default 默认状态--> </selector> 本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/3731190.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

【读书笔记《Android游戏编程之从零开始》】8.Android 游戏开发常用的系统控件(系统控件常见问题)

Android 中常用的计量单位 Android有时候需要一些计量单位,比如在布局Layout文件中可能需要指定具体单位等。 常用的计量单位有:px、dip(dp)、sp,以及一些不常用的pt、in、mm。下面详细介绍下这些计量单位之间的区别和联系。 in:英寸(长度单位); mm:毫米(长度单位); pt:磅/点,1/72英寸(一个标准的长度单位); sp:全名 scaled pixels-best for text size,放大像素,与刻度无关,可以根据用户的字体大小就行缩放,主要用来处理字体的大小; px:屏幕中的像素; dip(dp):设备独立像素,一种基于屏幕密度的抽象单位;因为不通设备中有不同的显示效果,所以为了解决在不通分辨率手机上运行不至于相差太大的问题,引入了dip计量单位,这种计量单位与移动设备硬件无关。 说道密度,这里简单介绍下。手机密度值(Density)表示每英寸有多少个显示点,与手机的分辨率是两个概念,但是分辨率与密度之间又互相关联,两者转换公式为: 密度值是120,屏幕实际分辨率为:240px×400px(两个点对应一个分辨率); 密度值是160,屏幕实际分辨率为:320px×533px(3个点对应两个分辨率); 密度值是240,屏幕实际分辨率为:480px×800px(一个点对应一个分辨率)。 比如,QVGA与WQVGA屏的密度值是120,HVGA屏密度值是160,WVGA屏密度值是240. res资源目录,因为运行的设备的不同,对应的资源文件目录也不同。其真正的原因是,资源目录是根据密度的不同来进行划分的: 密度值是120,对应的资源目录是drawable-ldpi; 密度值是160,对应的资源目录是drawable-mdpi; 密度值是240,对应的资源目录是drawable-hdpi。 根据以上介绍,在布局中应该尽量使用dip(dp)作为单位;而定义作为文字大小的单位则推荐使用sp。 Context Context 类是一个抽象类,它的子类很多,比如 Activity 、 TabActivity 、Service 等。很多方法中需要传入 Context 参数才可实例对象,例如 Toast 实例对象时,第一个对象需传入 Context 对象。其实 Context 从字面上可以理解为类似于句柄,联系上下文的意思。因为 Activity 是 Context 的子类,所以一般在 Activity 中使用 Context 的时候,可以用 this 来代替,但是如果在内部类中(如利用内部类使用监听组件),就不能使用 this 来代替 Context ,而是使用 "ActName.this" ,这里的 ActName 指的是 Activity 类的类名。 在Android中的 Context 可以有很多操作,但是最主要的功能是加载和访问资源,具体可以看下官方文档:http://developer.android.com/reference/android/content/Context.html Resources与getResources 在 Android 资源(Resource)都会自动由 R.java 资源文件生成对应的静态 ID ,通过 R 资源文件对资源生成的 ID 来引用。这样在资源需要修改的时候,就不用去程序源代码中修改,直接修改对应res下的资源文件即可。 在源代码中,如果需要对资源目录下的 string.xml 中定义的字符串变量进行访问,只需要通过 getResources 的方式引用即可。 例如,需要引用 string.xml 中的一个字符串,其变量为 "hello_world",获取方式如下: getResources().getString(R.string.hello_world); 再如需要引用 drawable 目录下的一张名为"goodby_times.png"的图片,获取方式如下: getResources().getDrawable(R.drawable.goodby_times); 当然一些函数不仅支持传入 String 类型,也支持传入引用 ID 。例如 TextView 中的setText() 函数,这个方法不仅支持传入 String 类型,还支持 R 文件引用 ID 的参数。"R.string.strName"中的 strName 表示在 string.xml 中定义的字符串在 R 资源文件中生成的对应 ID 索引。 findViewById与LayoutInflater LayoutInflater 的作用类似于 findViewById(),两者不同之处在于 LayoutInflater 是用来实例化 xml 布局文件中的布局:而 findViewById,顾名思义,是通过 ID 来找到 xml 布局文件中定义的组件,比如 EditText、TextView、Button 等等。 关于用 LayoutInflater 来实例布局的方式有两种: 1.通过传入 Context 参数来获得 LayoutInflater 实例,然后调用 LayoutInflater 类中的 inflate 函数来得到布局实例。 LayoutInflater inflater = LayoutInflater.from(Context context) ; View view = inflater.inflate(R.layout.activity_main, null); 2.通过系统服务来获取到 LayoutInflater 实例,然后调用 LayoutInflater 类中的 inflate 函数来得到布局实例。 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.activity_main, null); 尽管实例布局的形式不同,但是这两种布局方式的性质没有区别。 多个Activity之间跳转/退出/传递数据操作 主要代码: MainActivity.class OtherActivity.class PS:记得在 AndroidManifest.xml 文件中声明新建的Activity,不然应用会由于找不到活动而报异常。 <!-- 下面是注册新建的Activity(OtherActivity) --> <activity android:name="yc.example.activityex.OtherActivity"></activity> 横竖屏切换处理的三种方式 Android 手机中运行应用的时候,一般用户都是竖屏,但是如果突然将手机横屏,那么很可能就会造成程序出现异常,因为在 Android 中每次屏幕切换都会重启当前的 Activity 。这种情况下,异常的解决方式有一下三种。 1.锁定横竖屏切换 此方式只需要在 AndroidManifest.xml 文件中,对 Activity 定义屏幕方向属性只能为横屏或者竖屏即可 将屏幕固定为竖屏显示 <activity android:screenOrientation="portrait"> 将屏幕固定为横屏显示 <activity android:screenOrientation="landscape"> 其他参数: "unspecified":默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向. "landscape":横屏显示(宽比高要长) "portrait":竖屏显示(高比宽要长) "user":用户当前首选的方向 "behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的) "sensor":有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。 "nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。 因为一个 Android 应用程序中可能会有多个 Activity ,那么可以根据需要去配置每一个 Activity 的显示方式,如果不设置,默认可以横竖屏切换。 或者在源码中设置横竖屏: 设置竖屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 设置横屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 2.源代码中处理横竖屏切换事件 首先在AndroidManifest.xml中为Activity设置configChanges属性 android:configChanges="orientation|screenSize" 然后在对应的 Activity 源代码中重写 onConfigurationChanged() 函数即可。这样处理后,当横竖屏切换的时候,就会响应其 Activity 中的 onConfigurationChanged() 函数,然后对横竖屏做出判断处理。 package yc.example.helloworld; import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("--Main--", "onCreate"); textView=(TextView)findViewById(R.id.tv); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.i("--Main--", "onConfigurationChanged"); if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { textView.setText("当前屏幕为竖屏"); } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { textView.setText("当前屏幕为横屏"); } } } 此方式就不会在切换横竖屏的时候,Activity 默认重启了。 3.重写 onRestoreInstanceState() 与 onSaveInstanceState() 函数代码: @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i("YInfo", "onRestoreInstanceState()"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i("YInfo", "onSaveInstanceState()"); } 在屏幕切换横竖屏的时候,会响应 onSaveInstanceState() 函数,然后重启载入当前的 Activity ,最后响应 onRestoreInstanceState() 函数,所以可以通过重写这两个函数,进行屏幕横竖屏切换时的处理。 以上3种处理横竖屏切换方式,根据当前应用来进行选择。 本文转自叶超Luka博客园博客,原文链接:http://www.cnblogs.com/yc-755909659/p/3750030.html,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码