Java教程

随笔

本文主要是介绍随笔,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

界面一:

    

  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;
        }
    }
}

 

这篇关于随笔的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!