1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
2.在界面1做一个按钮开启浏览器访问百度
<?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=".Demo11Activity" android:orientation="vertical"> <Button android:id="@+id/anniu1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="anniu1" android:layout_gravity="center" /> <Button android:id="@+id/anniu11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="百度" android:layout_gravity="center"/> </LinearLayout>
<?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=".Demo22Activity" android:orientation="vertical"> <Button android:id="@+id/anniu2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="anniu2" android:layout_gravity="center"/> </LinearLayout>
<?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=".Demo33Activity" android:orientation="vertical"> <Button android:id="@+id/anniu3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="anniu3" android:layout_gravity="center" /> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Demo11Activity extends AppCompatActivity { private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo11); btn1=findViewById(R.id.anniu1); Button btn2=findViewById(R.id.anniu11); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(Demo11Activity.this,Demo22Activity.class); startActivityForResult(intent,1); finish(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); } }); } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Demo22Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo22); Button btn2=findViewById(R.id.anniu2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setAction("cn.itcast.START_ACTIVITY"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); } }); } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class Demo33Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo33); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Demo7Activity"> </activity> <activity android:name=".Demo33Activity"> <intent-filter> <action android:name="cn.itcast.START_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".Demo22Activity"></activity> <activity android:name=".Demo11Activity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Demo6Activity"></activity> <activity android:name=".Demo5Activity" /> <activity android:name=".Demo4Activity" /> <activity android:name=".Main2Activity" /> <activity android:name=".Demo3Activity" /> <activity android:name=".Demo2Activity" /> <activity android:name=".Demo1Activity" /> <activity android:name=".MainActivity" /> </application> </manifest>
3.2个edittext,4个按钮一个textview,实现简单计算器。
提示1:如何获取edittext上的数据?
String num1=((EditText)(findViewById(R.id.et1))).getText().toString();//获取et1上面的文本,并
转成字符串
提示2:字符串如何转int
int n1=Integer.parseInt(num1);
提示3:如何把计算结果显示在textview上?
TextView tv1=(TextView)findViewById(R.id.tv1);//获取控件
tv1.setText("1233213");//用settext方法赋值
<?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=".Demo7Activity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数字1" android:textSize="50dp"/> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入数字"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数字2" android:textSize="50dp"/> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入数字"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:layout_weight="1"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:layout_weight="1"/> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:layout_weight="1"/> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:layout_weight="1"/> </LinearLayout> <TextView android:id="@+id/tv3" android:layout_width="match_parent" android:layout_height="match_parent" android:text="结果为:" android:textSize="50dp"/> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import org.w3c.dom.Text; public class Demo7Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo7); //+ findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.et1))).getText().toString(); String num2=((EditText)(findViewById(R.id.et2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1+n2; TextView tv=(TextView)findViewById(R.id.tv3); tv.setText("结果为:"+n3); } }); //- findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.et1))).getText().toString(); String num2=((EditText)(findViewById(R.id.et2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1-n2; TextView tv=(TextView)findViewById(R.id.tv3); tv.setText("结果为:"+n3); } }); //* findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.et1))).getText().toString(); String num2=((EditText)(findViewById(R.id.et2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1*n2; TextView tv=(TextView)findViewById(R.id.tv3); tv.setText("结果为:"+n3); } }); /// findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.et1))).getText().toString(); String num2=((EditText)(findViewById(R.id.et2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1/n2; TextView tv=(TextView)findViewById(R.id.tv3); tv.setText("结果为:"+n3); } }); } }