zoukankan      html  css  js  c++  java
  • 毕设第一部分

    在value/res下建立string.xml想清楚自己的按钮名称,各种部件的名称和显示

    <resources>
    
       
        <string name = "app_name">haha</string>
        <string name = "app_name2">you are clicked me!!!</string>
        <string name = "button1">red</string>
        <string name = "button2">blue</string>
        <string name ="button3">text</string>
    </resources>

    再设计好布局:布局有线性布局linearLayout,包括两种现形布局,垂直vertical,水平horizontal

                                相对布局RelativeLayout:是按照组件之间的相对位置来布局的,如在某个组件的上下左右

                                表格布局TableLayout

                                 绝对布局:AbsoluteLayout 

    <?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" >
         <!-- 下面说明文本的id,长宽,还有显示的内容(要去string下面去找)-->
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           
            android:text="@string/app_name" />
           
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name2" />
       
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2.79" >
             <!-- 下面说明按钮的id,大小,以及按钮上面的内容 -->
            <Button
                android:id="@+id/button1"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:text="@string/button1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:text="@string/button2" />
    
            <Button
                android:id="@+id/button3"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:text="@string/button3" />
    
        </LinearLayout>
    
    </LinearLayout>

    src下面的java文件

    package org.test;
    
    import com.example.test1.R;
    
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Main_Activity extends Activity{//一定要继承Activity
         //创建属性
         private TextView text;
         private Button bt1;
         private Button bt2;
         private Button bt3;
         @Override
        //重写create方法
        public void onCreate(Bundle savedInstanceState)
        {   
            //继承create方法
            super.onCreate(savedInstanceState);
            //调用setContentView方法显示布局文件
            setContentView(R.layout.main_layout1);
            //在OnCreate方法中通过调用findViewById()方法可以根据标识ID获取TextView控件
            text=(TextView)findViewById(R.id.textView1);
            // 在Oncreate方法中通过调用findViewById()方法可以根据标识ID获取Button控件
            bt1=(Button) findViewById(R.id.button1);
            bt2=(Button) findViewById(R.id.button2);
            bt3=(Button) findViewById(R.id.button3);
            text = (TextView) findViewById(R.id.textView2);
            //注册监听事件,ClickListener是事件监听器,该监听器实现OnClickListener接口
           bt1.setOnClickListener(new Button.OnClickListener(){
                @Override
                //重写OnClickListener接口中的onClick方法
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //通过getWindow()获取屏幕窗口,再调用setBackgroundDrawableResource()方法设置背景色
                    //注意要在资源res/value文件夹下面建立color.xml来建立颜色
                    getWindow().setBackgroundDrawableResource(R.color.red);
                    
                }
    
            
            
            });
            bt2.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    getWindow().setBackgroundDrawableResource(R.color.blue);
                }
    
                
                
            });
            bt3.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //监听按钮事件,点击按钮触发下面的行为
                    text.setText("you are clicked me!!!!hahahahahahaha");
                }
    
                
                
            });
            String str="lalalalahahaha";
            //调用其setText方法设置显示的文字为字符串
            text.setText(str);
        }
        
        
    
    }
      

    color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="red">#FF0000</color>
        <color name="blue">#0000FF</color>
    </resources>

    再来看设置文件

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.test1"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />
        <!--application:应用程序相关信息,这个元素中的两个属性分别制定应用程序的图标和标题  -->
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
           <!-- activity:活动信息,其中两个属性表明Activity的类名和标题 -->
            <activity android:name="org.test.Main_Activity" >
                <!--intent-filter:找到该Activity的过滤器,此处的Action子元素表明该Activity是程序的入口  -->
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <!-- 表明在加载程序时运行 -->
                    <category android:name="android.intent.category.LAUNCHER"/>
                     
                </intent-filter>
            </activity>
        </application>
        
        
    
    </manifest>

    android中的事件监听器:

    OnclickListener单击事件

    OnFocusChangeListener焦点事件

    OnKeyListener按键事件

    OnTouchListener触碰事件

    OnCreateContextMenuListener创建上下文菜单事件

    OnCheckedChangeListener选项事件

  • 相关阅读:
    记账程序2.0
    安装redHat6.5详细图文教程
    redHat6设置ip地址
    如何开通B站的直播空间
    python实战技巧之两个字典,如何实现键同则值相加【不等长或等长】
    python实战技巧之去除列表中的空值元素
    python实战技巧之两个不等长列表让里面的数字一一对应地相加
    python实战技巧之两个列表实例中,如何让里面的数字一一对应地相加【对于两个列表是等长的情况】
    java基础_网络编程
    java基础_String
  • 原文地址:https://www.cnblogs.com/mmlovejj/p/4503899.html
Copyright © 2011-2022 走看看