Android开发第一天
1、主页面MainActivity.java的开发
//需求:1、版本号为1.0.0(在build.gradle里面设置) // 2、修改应用图标(在AndroidManifest设置) // 3、添加启动页面splashActivity(编写activity同时在AndroidManifest设置) // 4、点击启动进行页面跳转(intent和startActivity) package com.duelfijj.myapplication; import android.content.Intent; 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 { private EditText input_1; private EditText input_2; private Button btn_1; private Button mbtn_back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置页面布局 setContentView(R.layout.activity_main); btn_1=findViewById(R.id.btn_1); btn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { input_1=findViewById(R.id.input_1); String input1=input_1.getText().toString(); Double a=Double.parseDouble(input1); input_2=findViewById(R.id.input_2); String input2=input_2.getText().toString(); Double b=Double.parseDouble(input2); Double c=a+b; String d=String.valueOf(c); Toast.makeText(MainActivity.this,d,Toast.LENGTH_SHORT).show(); } }); mbtn_back=findViewById(R.id.btn_back); // 设置点击事件监听 mbtn_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击返回按钮后返回启动页面 Intent intent=new Intent(MainActivity.this,SplashActivity.class) ; startActivity(intent); } }); } }
2、主页面的布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入第一个数字" /> <EditText android:id="@+id/input_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入第二个数字" /> <EditText android:id="@+id/input_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="计算结果" /> <Button android:id="@+id/btn_back" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_back" /> </LinearLayout>
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
3、启动页面SplashActivity.java
package com.duelfijj.myapplication; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Switch; //继承方法 public class SplashActivity extends Activity { private Button mbtn_click; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //创建布局 setContentView(R.layout.activity_splash); // 如果报错就强制转换mbtn_click=(Button)findViewById(R.id.btn_click); mbtn_click=findViewById(R.id.btn_click); // 设置点击监听事件 mbtn_click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 点击button后将会转移到另一个页面 switch (v.getId()){ case R.id.btn_click: Intent intent = new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); break; } } }); } }
4、启动页面的布局
<?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:gravity="center" android:background="@color/splashbackgroundcolor"> <!--//gravity 让text居中--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/splash_text" android:textSize="46dp" android:textColor="@color/white" android:fontFamily="@string/splash_text"/> <Button android:id="@+id/btn_click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_text" android:textColor="@color/black" android:layout_marginTop="15dp" /> </LinearLayout>
5、AndriodManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.duelfijj.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/zd" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!--设置splashactivity为启动页面--> <activity android:name=".SplashActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--主页面的设置--> <activity android:name=".MainActivity"> </activity> </application> </manifest>

更多精彩