Android第七周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.content.Intent; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
public void click1(View view){ Intent intent=new Intent(this,oneActivity.class); startActivity(intent); } public void click2(View view){ Intent intent=new Intent(); intent.setAction("cn.it.ab_cd"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); } }
<?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"> <Button android:id="@+id/v1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click1" android:text="按钮1开启界面2" android:textColor="#FFC0CB" android:textSize="30sp" /> <Button android:id="@+id/v2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click2" android:text="按钮2开启界面3" android:textSize="30sp" android:textColor="#DDA0DD" /> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class oneActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".oneActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面2" android:textSize="40dp" android:textColor="#87CEFA" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class twoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); } }
<?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=".twoActivity"> <intent-filter> <action android:name="cn.it.ab_cd"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".oneActivity" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".twoActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面3" android:textSize="40dp" android:textColor="#9370DB" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
2.在界面1做一个按钮开启浏览器访问百度
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; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click1(View view){ Intent intent=new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" > <Button android:id="@+id/b1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FFC0CB" android:text="按钮1" android:textSize="40sp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:onClick="click1"/> </RelativeLayout>
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方法赋值
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.c1).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.b1))).getText().toString(); String num2=((EditText)(findViewById(R.id.b2))).getText().toString(); int a1= Integer.parseInt( num1 ); int a2=Integer.parseInt( num2 ); int a3=a1+a2; TextView d1=findViewById( R.id.d1 ); d1.setText( "="+a3); } }); findViewById(R.id.c2).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.b1))).getText().toString(); String num2=((EditText)(findViewById(R.id.b2))).getText().toString(); int a1= Integer.parseInt( num1 ); int a2=Integer.parseInt( num2 ); int a3=a1-a2; TextView d1=findViewById( R.id.d1 ); d1.setText( "="+a3 ); } }); findViewById(R.id.c3).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.b1))).getText().toString(); String num2=((EditText)(findViewById(R.id.b2))).getText().toString(); int a1= Integer.parseInt( num1 ); int a2=Integer.parseInt( num2 ); int a3=a1*a2; TextView d1=findViewById( R.id.d1 ); d1.setText( "="+a3 ); } }); findViewById(R.id.c4).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { String num1=((EditText)(findViewById(R.id.b1))).getText().toString(); String num2=((EditText)(findViewById(R.id.b2))).getText().toString(); int a1= Integer.parseInt( num1 ); int a2=Integer.parseInt( num2 ); int a3=a1/a2; TextView d1=findViewById( R.id.d1 ); d1.setText( "="+a3 ); } }); } }
<?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" android:background="#ADD8E6"> <TextView android:id="@+id/a1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算器" android:textSize="30sp" android:textColor="#FFB6C1"/> <EditText android:id="@+id/b1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入数字" /> <EditText android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入数字"/> <Button android:id="@+id/c1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="+" android:textSize="30sp" android:background="#D8BFD8" /> <Button android:id="@+id/c2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="-" android:textSize="30sp" android:background="#D8BFD8" /> <Button android:id="@+id/c3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="*" android:textSize="30sp" android:background="#D8BFD8" /> <Button android:id="@+id/c4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="/" android:textSize="30sp" android:background="#D8BFD8" /> <TextView android:id="@+id/d1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:textSize="30dp"/> </LinearLayout>