第七周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
com.example.wyn; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button) findViewById(R.id.btn1); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(MainActivity.this,MainActivity2.class); startActivity(i); } }); } } package com.example.wyn; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity2 extends Activity { Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2); btn1=(Button) findViewById(R.id.btn2); btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(); i.setAction("cn.itcast.startactivity"); i.addCategory("android.intent.category.DEFAULT"); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_activity2, menu); return true; } } package com.example.wyn; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity3 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity3); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_activity3, menu); return true; } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"/> </LinearLayout> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity2" > <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"/> </LinearLayout> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity3" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3"/> </LinearLayout>
2.在界面1做一个按钮开启浏览器访问百度
package com.example.wyn; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn; Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button) findViewById(R.id.btn1); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(MainActivity.this,MainActivity2.class); startActivity(i); } }); btn2=(Button) findViewById(R.id.btnllq); btn2.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(); i.setAction("android.intent.action.VIEW"); i.setData(Uri.parse("http://www.baidu.com")); startActivity(i); } }); } }
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.aswork1; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity3 extends AppCompatActivity { EditText et1; EditText et2; Button btn1; Button btn2; Button btn3; Button btn4; TextView tv1; String str1; String str2; int a; int b; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); et1=findViewById(R.id.et1); et2=findViewById(R.id.et2); btn1=findViewById(R.id.btn1); btn2=findViewById(R.id.btn2); btn3=findViewById(R.id.btn3); btn4=findViewById(R.id.btn4); tv1=findViewById(R.id.tv1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { str1=et1.getText().toString(); str2=et2.getText().toString(); a=Integer.parseInt(str1); b=Integer.parseInt(str2); String str3=""+(a+b); tv1.setText(str3); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { str1=et1.getText().toString(); str2=et2.getText().toString(); a=Integer.parseInt(str1); b=Integer.parseInt(str2); String str3=""+(a-b); tv1.setText(str3); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { str1=et1.getText().toString(); str2=et2.getText().toString(); a=Integer.parseInt(str1); b=Integer.parseInt(str2); String str3=""+(a*b); tv1.setText(str3); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { str1=et1.getText().toString(); str2=et2.getText().toString(); a=Integer.parseInt(str1); b=Integer.parseInt(str2); String str3=""+(a/b); tv1.setText(str3); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl_1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity3" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/et1"/> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/et2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn1" android:text="加"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn2" android:text="减"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn3" android:text="乘"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn4" android:text="除"/> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/tv1"/> </LinearLayout>