安卓第七周作业
1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3
public class MainActivity extends AppCompatActivity { private Button b_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text71); b_1=(Button) findViewById(R.id.b_1); b_1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); Toast.makeText(MainActivity.this,"已开启界面2",Toast.LENGTH_LONG).show(); } }); }
public class Main2Activity extends AppCompatActivity { private Button b_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); b_2=(Button) findViewById(R.id.b_2); b_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setAction("android.intent.action.MAIN3"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); Toast.makeText(Main2Activity.this,"已开启界面3",Toast.LENGTH_LONG).show(); } }); } }
public class Main3Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); } }
<?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=".Main3Activity"> <intent-filter> <action android:name="android.intent.action.MAIN3" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".Main2Activity" /> <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" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/b_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击开启界面2" android:layout_centerInParent="true"/> </RelativeLayout>
<?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=".Main2Activity"> <Button android:id="@+id/b_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击开启界面3" android:layout_centerInParent="true"/> </RelativeLayout>
<?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=".Main3Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是界面3" android:textSize="20dp" android:textColor="#000000" android:layout_centerInParent="true"/> </RelativeLayout>
2.在界面1做一个按钮开启浏览器访问百度
public void onClick2(View v){ Intent intent=new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); Toast.makeText(MainActivity.this,"已开启百度",Toast.LENGTH_LONG).show(); }
<?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"> <Button android:id="@+id/b_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击开启界面2" android:layout_centerInParent="true"/> <Button android:id="@+id/b_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击打开百度" android:layout_below="@+id/b_1" android:layout_centerHorizontal="true" android:onClick="onClick2"/> </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方法赋值
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text71); findViewById(R.id.b_1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num1=((EditText)(findViewById(R.id.ediText1))).getText().toString(); String num2=((EditText)(findViewById(R.id.ediText2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1+n2; TextView tv=(TextView)findViewById(R.id.tv); tv.setText("计算结果是:"+n3); Toast.makeText(MainActivity.this,"加法结果"+n3,Toast.LENGTH_LONG).show(); } }); findViewById(R.id.b_2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num1=((EditText)(findViewById(R.id.ediText1))).getText().toString(); String num2=((EditText)(findViewById(R.id.ediText2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1-n2; TextView tv=(TextView)findViewById(R.id.tv); tv.setText("计算结果是:"+n3); Toast.makeText(MainActivity.this,"减法结果"+n3,Toast.LENGTH_LONG).show(); } }); findViewById(R.id.b_3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num1=((EditText)(findViewById(R.id.ediText1))).getText().toString(); String num2=((EditText)(findViewById(R.id.ediText2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1*n2; TextView tv=(TextView)findViewById(R.id.tv); tv.setText("计算结果是:"+n3); Toast.makeText(MainActivity.this,"乘法结果"+n3,Toast.LENGTH_LONG).show(); } }); findViewById(R.id.b_4).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String num1=((EditText)(findViewById(R.id.ediText1))).getText().toString(); String num2=((EditText)(findViewById(R.id.ediText2))).getText().toString(); int n1=Integer.parseInt(num1); int n2=Integer.parseInt(num2); int n3=n1/n2; TextView tv=(TextView)findViewById(R.id.tv); tv.setText("计算结果是:"+n3); Toast.makeText(MainActivity.this,"除法结果"+n3,Toast.LENGTH_LONG).show(); } }); }
<?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:gravity="center" android:orientation="vertical"> <EditText android:id="@+id/ediText1" android:layout_width="300dp" android:layout_height="70dp" android:layout_marginBottom="10dp" android:hint="请输入数1" android:textSize="30dp"/> <EditText android:id="@+id/ediText2" android:layout_below="@id/ediText1" android:layout_width="300dp" android:layout_height="70dp" android:layout_marginBottom="10dp" android:hint="请输入数2" android:textSize="30dp"/> <Button android:id="@+id/b_1" android:layout_below="@id/ediText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textSize="20dp"/> <Button android:id="@+id/b_2" android:layout_below="@id/ediText2" android:layout_toRightOf="@id/b_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="20dp"/> <Button android:id="@+id/b_3" android:layout_below="@id/ediText2" android:layout_toRightOf="@id/b_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:textSize="20dp"/> <Button android:id="@+id/b_4" android:layout_below="@id/ediText2" android:layout_toRightOf="@id/b_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textSize="20dp"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:layout_below="@id/b_1" android:textSize="30dp"/> </RelativeLayout>