第七周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
2.在界面1做一个按钮开启浏览器访问百度
package com.example.app6; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class Activity3 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity3); } }
<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" tools:context=".Main2Activity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面2" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式意图开启界面3" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启浏览器访问百度" /> </LinearLayout>
<RelativeLayout 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" tools:context=".Main3Activity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="界面3" /> </RelativeLayout>
package com.example.myapplication; import android.net.Uri; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); } }
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; import java.time.Instant; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Button btn2=findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setClass(Main2Activity.this,Main3Activity.class); startActivity(intent); } }); Button btn3=findViewById(R.id.btn3); btn3.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.os.Bundle; public class Main3Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); } }
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方法赋值
RelativeLayout 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" tools:context=".MainActivity" > <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入第一个数" android:textSize="50dp" android:background="#32FF00" android:layout_marginBottom="100dp" /> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入第二个数" android:textSize="50dp" android:background="#00FFF6" android:layout_below="@id/et1" android:layout_marginBottom="100dp" /> <Button android:layout_below="@id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b1" android:text="+" android:textSize="50dp" android:textColor="#FF0000" /> <Button android:layout_below="@id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b2" android:text="-" android:textSize="50dp" android:layout_toRightOf="@id/b1" /> <Button android:layout_below="@id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b3" android:text="*" android:textSize="50dp" android:layout_toRightOf="@id/b2" /> <Button android:layout_below="@id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b4" android:textSize="50dp" android:text="/" android:layout_toRightOf="@id/b3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:layout_below="@id/b1" android:text="结果是" android:textSize="50dp" /> </RelativeLayout> package com.example.mya; 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; public class ThreeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); //加法运算 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 n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1+n2; TextView jieguo=(TextView)findViewById(R.id.A2); jieguo.setText("计算结果是:"+n3); } }); //减法运算 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 n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1-n2; TextView jieguo=(TextView)findViewById(R.id.A2); jieguo.setText("计算结果是:"+n3); } }); //乘法运算 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 n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1*n2; TextView jieguo=(TextView)findViewById(R.id.A2); jieguo.setText("计算结果是:"+n3); } }); //除法运算 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 n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1/n2; TextView jieguo=(TextView)findViewById(R.id.A2); jieguo.setText("计算结果是:"+n3); } }); } } package com.example.myapplication; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // + findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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 tv1=(TextView)findViewById(R.id.tv); tv1.setText("结果是"+n3); } }); // - findViewById(R.id.b2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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 tv1=(TextView)findViewById(R.id.tv); tv1.setText("结果是"+n3); } }); // * findViewById(R.id.b3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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 tv1=(TextView)findViewById(R.id.tv); tv1.setText("结果是"+n3); } }); // / findViewById(R.id.b4).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub 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 tv1=(TextView)findViewById(R.id.tv); tv1.setText("结果是"+n3); } }); } }