Android入门基础知识
最后更新 2021-02-28 17:46 星期日 所属:
安卓教程 浏览:1157
Android学习心得体会-第一篇
Android常见问题
1、撰写Activity.java文档时一定要在AndroidManifest.xml中申请注册一下才可以一切正常运作
2、res/layout文件目录中的xml文件夹名称一定要应用小写字母取名
3、必须应用自定的颜色值和自定的字符串数组(比如特殊字体:带下横线的文本)时,只必须在res/values 文件目录中界定就可以,比如:
1)自定color >>加上一个colors.xml文档
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorgrey">#6B6B6B</color> </resources>
2)自定strings >>加上strings.xml文档
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TestActivity!</string> <string name="app_name">appName</string> <string name="protocol"><u>《***软件许可协议》</u></string> </resources>
4、不必乱往res文件目录下的文件夹名称中加上內容,由于这种文档会在R.java文档中被引入,稍不留神便会出現难以发觉的不正确,造成程序流程没法运作,浪费时间,错过良好的机会哦!。
5、合理布局文档中常见的标识有下列好多个,要灵便应用
<RelativeLayout ></RelativeLayout>
<LinearLayout ></LinearLayout>
<ImageButton />
<ImageView />
<TextView />
实例:下列合理布局编码可完成带标志的可点一下按键(home.xml)
<?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" android:background="@drawable/background_main" > <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:background="#393a3e" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="幸福苑" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_login" android:layout_width="match_parent" android:layout_height="60dp" android:paddingLeft="10dp" android:layout_marginTop="20dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" android:clickable="true" style="?android:attr/actionButtonStyle" > <LinearLayout android:layout_width="50dp" android:layout_height="50dp" android:orientation="horizontal" > <ImageButton android:id="@ id/id_tab_login_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="left|center_vertical" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="@string/login_text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:textSize="12dp" android:text="打开你的幸福快乐之行" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@ id/id_tab_pension_consult" android:layout_width="match_parent" android:layout_height="40dp" android:paddingLeft="10dp" android:layout_marginTop="20dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" android:clickable="true" > <!-- android:clickable="false" 是为了更好地避免 ImageButton提取了触碰事情 ,这儿事情要给它的上一级linearlayout--> <ImageButton android:id="@ id/id_tab_pension_consult_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="养老服务资询" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_server_center" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@ id/id_tab_server_center_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="服务站" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_recipe" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" style="?android:attr/actionButtonStyle" > <ImageButton android:id="@ id/id_tab_recipe_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="营养餐食谱" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_medical_examination_data" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" style="?android:attr/actionButtonStyle" > <ImageButton android:id="@ id/id_tab_medical_examination_data_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="常规体检数据信息" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_medical" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@ id/id_tab_medical_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="诊疗接诊" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_community_video_monitoring" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@ id/id_tab_community_video_monitoring_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="社区养老服务视频监控系统" /> </LinearLayout> <LinearLayout android:id="@ id/id_tab_feedback" android:layout_width="match_parent" android:layout_height="40dp" android:paddingLeft="10dp" android:layout_marginTop="1dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@ id/id_tab_feedback_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="反馈意见" /> </LinearLayout> </LinearLayout>
点击事件的加上方式(HomeActivity.java)
package com.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.LinearLayout; public class HomeActivity extends Activity implements android.view.View.OnClickListener { //四个Tab,每一个Tab包括一个按键 private LinearLayout myTabLogin; private LinearLayout myTabPensionConsulting; private LinearLayout myTabServerCenter; private LinearLayout myTabRecipe; private LinearLayout myTabMedicalExaminationData; private LinearLayout myTabMedical; private LinearLayout myTabCommunityVideoMonitoring; private LinearLayout myTabFeedback; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);//除掉菜单栏 setContentView(R.layout.home); init(); } /* * 复位设定 */ private void init() { //复位 LinearLayout myTabLogin = (LinearLayout) findViewById(R.id.id_tab_login); myTabPensionConsulting = (LinearLayout) findViewById(R.id.id_tab_pension_consult); myTabServerCenter = (LinearLayout) findViewById(R.id.id_tab_server_center); myTabRecipe = (LinearLayout) findViewById(R.id.id_tab_recipe); myTabMedicalExaminationData = (LinearLayout) findViewById(R.id.id_tab_medical_examination_data); myTabMedical = (LinearLayout) findViewById(R.id.id_tab_medical); myTabCommunityVideoMonitoring = (LinearLayout) findViewById(R.id.id_tab_community_video_monitoring); myTabFeedback = (LinearLayout) findViewById(R.id.id_tab_feedback); /* * 监视并解决点击事件 * 实际的解决方式在onClick(View v)方式中 */ myTabLogin.setOnClickListener(this); myTabPensionConsulting.setOnClickListener(this); myTabServerCenter.setOnClickListener(this); myTabRecipe.setOnClickListener(this); myTabMedicalExaminationData.setOnClickListener(this); myTabMedical.setOnClickListener(this); myTabCommunityVideoMonitoring.setOnClickListener(this); myTabFeedback.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.id_tab_login: Intent intent = new Intent(HomeActivity.this, LoginActivity.class); HomeActivity.this.startActivity(intent); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_medical_examination_data: Intent intent1 = new Intent(HomeActivity.this, MedicalExamDataActivity.class); HomeActivity.this.startActivity(intent1); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_recipe: Intent intent11 = new Intent(HomeActivity.this, RecipeActivity.class); HomeActivity.this.startActivity(intent11); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_pension_consult: Intent intent2 = new Intent(HomeActivity.this, TodoActivity.class); HomeActivity.this.startActivity(intent2); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_server_center: Intent intent22 = new Intent(HomeActivity.this, ServerCenterActivity.class); HomeActivity.this.startActivity(intent22); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_medical: Intent intent3 = new Intent(HomeActivity.this, MedicalActivity.class); HomeActivity.this.startActivity(intent3); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_community_video_monitoring: Intent intent33 = new Intent(HomeActivity.this, TodoActivity.class); HomeActivity.this.startActivity(intent33); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_feedback: Intent intent4 = new Intent(HomeActivity.this, TodoActivity.class); HomeActivity.this.startActivity(intent4); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; default: break; } } }