kotlin在主程序中提交数据到fragment中并显示的简单练习

MainActivity.kt

 

package com.example.administrator.kt_test

import android.app.FragmentManager
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.EditText

class MainActivity : AppCompatActivity() {
//    private var mrecycle_1=findViewById(R.id.mrecycle_1)
//    private var mylm= LinearLayoutManager(this)
//        var et_1=findViewById(R.id.et_1)
        
private lateinit varmanager:FragmentManager
    private lateinit var transaction:Any
    private lateinit var et_1:EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        et_1=findViewById(R.id.et_1)
        //结果引入
        
manager=getFragmentManager()
        var transaction=manager.beginTransaction()
         transaction.add(R.id.content_layout,ResultFrag())
        transaction.commit()



    }

public fun sendValue(view:View){
    var info=et_1.getText().toString().trim()//trim()函数去掉头尾的空格
    
varresultFragment=ResultFrag()
    var bundle=Bundle()
    bundle.putString("info",info)
    resultFragment.setArguments(bundle)
    manager=getFragmentManager()
    var transaction=manager.beginTransaction()
    transaction.replace(R.id.content_layout,resultFragment)
    transaction.commit()
}
}

ResultFrag.kt

package com.example.administrator.kt_test

import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

/**
 * Created by Administrator on 2017/11/16.
 */

public class ResultFrag:Fragment(){
    lateinit var tv_2:TextView
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {

        var view= inflater!!.inflate(R.layout.fragment_result,null)
        tv_2=view.findViewById(R.id.tv_2)//getArgument()->bundle,
        
varbundle: Bundle? = getArguments()
        if(bundle!=null) {
            var info: String = bundle!!.getString("info")
            tv_2.setText(info)
        }

        return view
    }




}

 

 

 

 

 

 

 

 

 

Activity_main.xml

 

xml version="1.0"encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns: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.administrator.kt_test.MainActivity"
>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.256"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.093"
/>

    <LinearLayout
        android:id="@+id/content_layout"
        android:layout_width="350dp"
        android:layout_height="198dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.972"
>LinearLayout>

    <Button
        android:id="@+id/btn_1"
        android:layout_width="109dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="52dp"
        android:text="submit"
        app:layout_constraintBottom_toTopOf="@+id/content_layout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.969"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.068"
/>

    <EditText
        android:id="@+id/et_1"
        android:layout_width="213dp"
        android:layout_height="45dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="content:"
        app:layout_constraintBottom_toTopOf="@+id/content_layout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.141"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
/>

android.support.constraint.ConstraintLayout>

 

 

你可能感兴趣的:(kotlin)