Android开发--fragment中Button点击切换Activity

来源:互联网 发布:淘宝收藏宝贝没反应 编辑:程序博客网 时间:2024/06/07 02:07

页面贴图:fragment_fragment1



布局文件:fragment_fragment1.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20sp"            android:text="用户名:"            android:id="@+id/name" />        <EditText            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textSize="20sp"            android:hint="Name"            android:id="@+id/input"            android:inputType="number" />    </LinearLayout>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="New Button"        android:clickable="true"        android:id="@+id/button"        android:layout_gravity="center_horizontal"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="20sp"        android:hint="提示"        android:id="@+id/remind" /></LinearLayout>


Java文件:Fragment1.java

package com.example.lenovo.sitp;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;




public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
                            @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_fragment1,container,false);
        Button btn = (Button)view.findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText in=(EditText)view.findViewById(R.id.input);
                TextView out=(TextView)view.findViewById(R.id.remind);
                String s=in.getText().toString();
                if(s.equals("123")){
                    startActivity(new Intent(getActivity(),run.class));
                }else{
                    out.setText("错误,正确为:123");
                }
            }
        });
        return view;


    }
}



0 0
原创粉丝点击