第七周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
<?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=".MainActivity"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启界面2"/> <Button android:id="@+id/bt_2" android:layout_below="@id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启界面3"/> </RelativeLayout>
<?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=".MainActivity2"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="界面2"/> </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=".MainActivity3"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="界面3" android:textSize="20dp"/> </LinearLayout>
package com.example.chap7; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; 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=(Button) findViewById(R.id.bt_1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); } }); } }
package com.example.chap7; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Button btn2=(Button) findViewById(R.id.bt_2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setAction("com.ymz.second"); startActivity(intent); } }); } }
package com.example.chap7; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); } }
2.在界面1做一个按钮开启浏览器访问百度
<?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=".MainActivity"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启界面2"/> <Button android:id="@+id/bt_2" android:layout_below="@id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="百度" /> </RelativeLayout>
package com.example.chap7; 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 MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button)findViewById(R.id.bt_2); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); } }); } 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); } }
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"?> <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=".MainActivity"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算器" android:textSize="25dp" android:layout_margin="5dp"/> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入第一个数" android:layout_marginTop="75dp" /> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入第二个数" android:layout_below="@id/et_1" /> <Button android:id="@+id/b_1" android:layout_width="70dp" android:layout_height="50dp" android:text="+" android:textSize="25dp" android:background="#AAA898" android:layout_marginTop="200dp" android:layout_marginLeft="22dp"/> <Button android:id="@+id/b_2" android:layout_width="70dp" android:layout_height="50dp" android:text="-" android:textSize="25dp" android:background="#AAA898" android:layout_toRightOf="@id/b_1" android:layout_marginTop="200dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/b_3" android:layout_width="70dp" android:layout_height="50dp" android:text="*" android:textSize="25dp" android:background="#AAA898" android:layout_toRightOf="@id/b_2" android:layout_marginTop="200dp" android:layout_marginLeft="10dp"/> <Button android:id="@+id/b_4" android:layout_width="70dp" android:layout_height="50dp" android:text="/" android:textSize="25dp" android:background="#AAA898" android:layout_toRightOf="@id/b_3" android:layout_marginTop="200dp" android:layout_marginLeft="10dp"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="结果是" android:layout_marginTop="300dp" android:layout_marginLeft="10dp"/> </RelativeLayout>
package com.example.chap73; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; 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.b_1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1= Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1+n2; TextView tv1=findViewById(R.id.tv2); tv1.setText("结果是"+sum); } }); findViewById(R.id.b_2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1-n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } }); findViewById( R.id.b_3 ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1*n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); findViewById( R.id.b_4 ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1*n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); findViewById( R.id.b_4).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String num1=((EditText)(findViewById( R.id.et_1 ))).getText().toString(); String num2=((EditText)(findViewById( R.id.et_2 ))).getText().toString(); int n1=Integer.parseInt( num1 ); int n2=Integer.parseInt( num2 ); int sum=n1/n2; TextView tv1=findViewById( R.id.tv2 ); tv1.setText("结果是"+sum); } } ); } }