Java教程

第八次

本文主要是介绍第八次,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }
    public void btn3(View view){
        Intent intent=new Intent();
        TextView textView=(TextView)(findViewById(R.id.et_3));
        int price=Integer.parseInt(textView.getText().toString());
        intent.putExtra("price", price);
        setResult(222, intent);
        finish();
    }

}
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }
    public void btn3(View view){
        Intent intent=new Intent();
        TextView textView=(TextView)(findViewById(R.id.et_3));
        int price=Integer.parseInt(textView.getText().toString());
        intent.putExtra("price", price);
        setResult(222, intent);
        finish();
    }

}
<?xml version="1.0" encoding="utf-8"?>
<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=".Main3Activity">
    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="充值金额"
        android:id="@+id/c1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/an3"
        android:text="完成"
        android:onClick="btn3"/>



</RelativeLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    String s1="",s2="",s3="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button  button=(Button)findViewById(R.id.an1);
        CheckBox cb1=(CheckBox)findViewById(R.id.f1);
        CheckBox cb2=(CheckBox)findViewById(R.id.f2);
        CheckBox cb3=(CheckBox)findViewById(R.id.f3);
        cb1.setOnCheckedChangeListener(this);
        cb2.setOnCheckedChangeListener(this);
        cb3.setOnCheckedChangeListener(this);


    }
    public void btn1(View view){
        Intent intent=new Intent(MainActivity.this,Main2Activity.class);
        EditText editText=(EditText)findViewById(R.id.et1);
        String name=editText.getText().toString();
        intent.putExtra("name",name);


        String text=s1+s2+s3;
        intent.putExtra("text",text);


        startActivity(intent);




    }

    @Override
    public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
        switch (cb.getId()) {
            case R.id.f1:
                if(isChecked)
                    s1+="编程";
                else s1="";
                break;
            case R.id.f2:
                if(isChecked)
                    s2+=" 下棋";
                else s2="";
                break;
            case R.id.f3:
                if(isChecked)
                    s3+=" 唱歌";
                else s3="";
                break;

            default:
                break;
        }
    }
}
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        TextView textview=(TextView)findViewById(R.id.b1);
        TextView textView1=findViewById(R.id.b2);
        Intent intent=getIntent();
        String name1=intent.getStringExtra("name");
        String text="用户名:"+name1;
        textview.setText(text);

        String text1=intent.getStringExtra("text");
        String text2="兴趣爱好:"+text1;
        textView1.setText(text2);


    }
    public void btn2(View view){
        Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
        startActivityForResult(intent, 111);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intent);
        if(requestCode==111&&resultCode==222){

            int price1=intent.getIntExtra("price",0);
            TextView textView1=(TextView)(findViewById(R.id.b3));
            String text2="充值金额:"+price1;
            textView1.setText(text2);
        }
    }

}
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }
    public void btn3(View view){
        Intent intent=new Intent();
        TextView textView=(TextView)(findViewById(R.id.c1));
        int price=Integer.parseInt(textView.getText().toString());
        intent.putExtra("price", price);
        setResult(222, intent);
        finish();
    }

}

 

 

 

 

 

 

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