ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第七周作品

2021-10-09 13:00:57  阅读:174  来源: 互联网

标签:findViewById 第七 int void 作品 Override id View


1.三个界面,界面1点击按钮使用显式意图开启界面2.
界面2点击按钮隐式意图开启界面3

2.在界面1做一个按钮开启浏览器访问百度

<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=".MainActivity"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显式意图开启界面2"
        android:onClick="click1"
        />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐式意图开启界面3"
        android:onClick="click2"
        />
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启浏览器访问百度"
        android:onClick="click3"
        />

</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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面2" />

</RelativeLayout>
<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=".ThirdActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面3" />

</RelativeLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.net.Uri;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("MainActivity", "调用oncreate");
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e("MainActivity", "调用onstart");
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e("MainActivity", "调用onresume");
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.e("MainActivity", "调用onpause");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e("MainActivity", "调用onstop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e("MainActivity", "调用ondestroy");
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.e("MainActivity", "调用onRestart");
    }

    //使用显式意图开启界面2
    public void click1(View view){
        Intent intent=new Intent(this,SecondActivity.class);
        startActivity(intent);

    }

    //使用隐式意图开启界面3
    public void click2(View view){
        Intent intent=new Intent();
        intent.setAction("cn.itcast.START_ACTIVITY");
        startActivity(intent);
    }

    public void click3(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方法赋值

package com.example.myapplication;

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.btn_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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.tv3);
                tv1.setText("结果是"+n3);
            }
        });
        //-
        findViewById(R.id.btn_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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.tv3);
                tv1.setText("结果是"+n3);
            }
        });
        //*
        findViewById(R.id.btn_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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.tv3);
                tv1.setText("结果是"+n3);
            }
        });
        //除
        findViewById(R.id.btn_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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.tv3);
                tv1.setText("结果是"+n3);
            }
        });
    }
}
<?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"
    android:layout_margin="20dp">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数字:"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_marginTop="15dp"/>
    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入数字"
        android:textSize="25sp"
        android:textColorHint="#888585"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:layout_toRightOf="@+id/tv1"/>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="数字:"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_marginTop="25dp"
        android:layout_below="@id/tv1"/>
    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入数字"
        android:textSize="25sp"
        android:textColorHint="#FF888585"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:layout_toRightOf="@+id/tv1"
        android:layout_below="@id/et1"/>
    <Button
        android:id="@+id/btn_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="+"
        android:textColor="#000000"
        android:textSize="60sp"
        android:layout_below="@id/et2"
        android:layout_marginTop="100dp"
        android:layout_margin="10dp"
        android:layout_alignLeft="@id/et2" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/et2"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="11dp"
        android:layout_toRightOf="@id/btn_1"
        android:text="-"
        android:textColor="#000000"
        android:textSize="60sp" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/btn_1"
        android:layout_margin="10dp"
        android:text="*"
        android:textColor="#000000"
        android:textSize="60sp"
        android:layout_alignLeft="@id/et2" />

    <Button
        android:id="@+id/btn_4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/btn_1"
        android:layout_alignTop="@id/btn_3"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="1dp"
        android:layout_toRightOf="@id/btn_3"
        android:text="/"
        android:textColor="#000000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_3"
        android:layout_margin="20dp"
        android:text="结果是:"
        android:textSize="30dp"
        android:textColor="#000000"
        android:textStyle="italic"/>
</RelativeLayout>

 

 

 

标签:findViewById,第七,int,void,作品,Override,id,View
来源: https://www.cnblogs.com/myh1027/p/15385158.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有