我的Android开发之旅(一)第一个程序Hello World

来源:互联网 发布:2017酒店行业市场数据 编辑:程序博客网 时间:2024/05/16 17:41

从现在开始我将要记录我在学习开发的过程中所学到的东西,以及遇到的困难,算是对我的一个监督。见证我的进步。也算是练打字了,希望自己早日摆脱眼看键盘。哈哈哈哈哈大笑大哭。就是不知道要到哪一年!!!!言归正传:Hello World大家都不陌生,也是学习开发的万年老梗安静。当然我接触的第一个Android开发程序也是Hello World。

新建属于自己的第一个程序:

耶耶耶耶耶。

整个人都不好了。其他的就默认吧!!

接下来就是我们大显身手的时候了,写代码:

res->layout->actvity_main.xml中写布局文件。

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?></span><LinearLayout 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.jerehedu.helloworld.MainActivity"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我的第一个程序!"        android:textAlignment="center"        android:textSize="30sp"        android:textColor="#0000ff"        android:id="@+id/textView" />    <TextView        android:id="@+id/show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="俗话说得好,工欲善其事,必先利其器。搭建开发环境,创建模拟器,模拟器会像手机一样"        android:textSize="20sp"        android:maxLines="2"        android:minLines="1"        android:ellipsize="end"        android:textColor="#ff0000"        /><EditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="请输入用户名"    android:drawableLeft="@mipmap/ic_launcher"    android:drawablePadding="20dp"    android:id="@+id/userName"    />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="textPassword"        android:hint="请输入密码"        android:drawableLeft="@mipmap/ic_launcher"        android:ems="10"        android:id="@+id/userPwd"        android:layout_gravity="center_horizontal" /><Button    android:id="@+id/btnLogin"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="登陆"    android:textSize="20sp"        android:textColor="#0000ff"/></LinearLayout>

为了看到程序运行的结果,在

java->com.jerehe.helloworld->MainActivity中写如下代码:

package com.jerehedu.helloworld;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //R:R文件是系统自动维护的文件。@LayoutRes int layoutResID 作用:注册id        Button btnLogin=(Button)findViewById(R.id.btnLogin);        final EditText userName=(EditText)findViewById(R.id.userName);        final  EditText userPwd=(EditText)findViewById(R.id.userPwd);        final TextView show =(TextView)findViewById(R.id.show);                //事件监听,当按下按钮时,显示“你点击了按钮”        btnLogin.setOnClickListener(new View.OnClickListener() {                                        @Override                                        public void onClick(View v) {                                            String name=userName.getText().toString();                                            String pwd=userPwd.getText().toString();                                            show.setText("用户名:"+name+";密码"+ pwd);                                        }                                    }        );    }    private class BtnLoginListener implements View.OnClickListener{        @Override        public void onClick(View v){            Toast.makeText(                    MainActivity.this,                    "你点击了按钮",                    Toast.LENGTH_SHORT            ).show();        }    }}


运行结果



我的偶像薛之谦:我的愿望是世界和平!

薛之谦的粉丝我:我的愿望是世界和平的同时变身美女程序员!哈哈哈哈




0 0
原创粉丝点击