ICode9

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

随笔

2021-10-14 01:01:46  阅读:31  来源: 互联网

标签:findViewById class Intent import android 随笔 id


界面一:

    

  XML:

<?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="com.example.hp.mainactivity.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:layout_weight="1"
            android:layout_margin="10dp"/>
        <EditText
            android:id="@+id/edt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:layout_margin="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:layout_weight="1"
            android:layout_margin="10dp"/>
        <EditText
            android:id="@+id/edt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:layout_margin="10dp"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择兴趣爱好:"
        android:layout_margin="10dp" />
    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="编程"/>
    <CheckBox
        android:id="@+id/cb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下棋"/>
    <CheckBox
        android:id="@+id/cb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="唱歌"/>

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="注册"
        android:layout_margin="100dp"/>

</LinearLayout>

  逻辑代码:

package com.example.hp.mainactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    String s1="",s2="",s3="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt1).setOnClickListener(this);
        CheckBox cb1=(CheckBox)findViewById(R.id.cb1);
        CheckBox cb2=(CheckBox)findViewById(R.id.cb2);
        CheckBox cb3=(CheckBox)findViewById(R.id.cb3);
        cb1.setOnCheckedChangeListener(this);
        cb2.setOnCheckedChangeListener(this);
        cb3.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,SecondActivity.class);
        String name=((EditText)(findViewById(R.id.edt1))).getText().toString();
        String account="用户名:"+name;
        String text=s1+" "+s2+" "+s3;
        intent.putExtra("account",account);
        intent.putExtra("text",text);
        startActivity(intent);

    }

    public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
        Intent intent=new Intent(this,SecondActivity.class);
        switch (cb.getId()) {
            case R.id.cb1:
                if(isChecked)
                    s1+="编程";
                else s1="";
                break;
            case R.id.cb2:
                if(isChecked)
                    s2+="下棋";
                else s2="";
                break;
            case R.id.cb3:
                if(isChecked)
                    s3+="唱歌";
                else s3="";
                break;

            default:
                break;
        }
    }



}

界面二:

      

  XML:

<?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="com.example.hp.mainactivity.SecondActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="20dp" />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:text="您选择的兴趣爱好为:"/>
    <TextView
        android:id="@+id/hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="我要充值"
        android:layout_margin="20dp" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>

  逻辑代码:

package com.example.hp.mainactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent1=getIntent();
        String name=intent1.getStringExtra("account");
        ((TextView)(findViewById(R.id.tv1))).setText(name);
        Intent intent2=getIntent();
        String text=intent2.getStringExtra("text");
        ((TextView)(findViewById(R.id.hobby))).setText(text);

        findViewById(R.id.bt2).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this,ThirdActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==2){
            String pay=data.getStringExtra("pay");
            ((TextView)(findViewById(R.id.tv2))).setText(pay);
        }
    }
}

界面三:

  XML:

<?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="com.example.hp.mainactivity.ThirdActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="500"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/bt_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1000"
        android:layout_gravity="center"/>

</LinearLayout>

  逻辑代码:

package com.example.hp.mainactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class ThirdActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        findViewById(R.id.bt_1).setOnClickListener(this);
        findViewById(R.id.bt_2).setOnClickListener(this);
        findViewById(R.id.bt_3).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_1:
                Intent intent1=new Intent(this,SecondActivity.class);
                intent1.putExtra("pay","充值金额为:100元");
                setResult(2,intent1);
                finish();
                break;
            case R.id.bt_2:
                Intent intent2=new Intent(this,SecondActivity.class);
                intent2.putExtra("pay","充值金额为:500元");
                setResult(2,intent2);
                finish();
                break;
            case R.id.bt_3:
                Intent intent3=new Intent(this,SecondActivity.class);
                intent3.putExtra("pay","充值金额为:1000元");
                setResult(2,intent3);
                finish();
                break;
        }
    }
}

 

标签:findViewById,class,Intent,import,android,随笔,id
来源: https://www.cnblogs.com/cui-Y-P/p/15404913.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

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

ICode9版权所有