zoukankan      html  css  js  c++  java
  • 五种方式实现点击按钮显示输入的信息

    1.内部类

    (1)MainActivity:

    package inspur.com.hello;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        private EditText et_pbone;
        private Button bt_show;
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //初始化
            et_pbone= (EditText) findViewById(R.id.et_phone);
            bt_show= (Button) findViewById(R.id.btn_show);
            textView=(TextView)findViewById(R.id.tv_show);
    
            //事件源绑定事件监听器
            bt_show.setOnClickListener(new Mylistener());
        }
    
        class Mylistener implements View.OnClickListener{
            @Override
            public void onClick(View v) {
                String num = et_pbone.getText().toString();
                textView.setText("您的手机号是"+num);
            }
        }
    }

    (2).显示布局main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="inspur.com.hello.MainActivity"
        android:orientation="vertical"
        >
    
        <EditText
            android:id="@+id/et_phone"
            android:hint="请输入手机号"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_show"
            android:text="显示您输入的手机号"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </LinearLayout>

    (3).AndroidManifest.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="inspur.com.hello">
     4 
     5     <application
     6         android:allowBackup="true"
     7         android:icon="@mipmap/ic_launcher"
     8         android:label="@string/app_name"
     9         android:supportsRtl="true"
    10         android:theme="@style/AppTheme">
    11         <activity android:name=".MainActivity">     /*此处设置MainActivity为主入口*/
    12             <intent-filter>
    13                 <action android:name="android.intent.action.MAIN" />
    14 
    15                 <category android:name="android.intent.category.LAUNCHER" />
    16             </intent-filter>
    17         </activity>
    18     </application>
    19 
    20 </manifest>

    2.外部类----注意更改:<activity android:name=".SActivity">

    package inspur.com.hello;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    /**
     * Created by Administrator on 2018/5/4.
     */
    public class SActivity extends Activity {
         // 声明
        private TextView textView;
        private Button button;
        private EditText editText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //初始化
            textView=(TextView) findViewById(R.id.tv_show);
            button = (Button) findViewById(R.id.btn_show);
            editText = (EditText) findViewById(R.id.et_phone);
            //事件绑定事件监听器
            button.setOnClickListener(new Mylistener(textView,editText));
    
        }
    }
    /*外部类*/
    class  Mylistener implements View.OnClickListener{
        private TextView tv;
        private EditText et;
    
        public Mylistener(TextView tv,EditText et) {
            this.tv=tv;
            this.et=et;
        }
    
        @Override
        public void onClick(View v) {
            String num = et.getText().toString();
            tv.setText("22您的手机号是"+num);
        }
    }

     3.Activity本身作为事件监听器----注意更改:<activity android:name=".SActivity">

    package inspur.com.hello;
    
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    /**
     * Activity本身作为事件监听器
     */
    public class TActivity extends Activity implements View.OnClickListener {
        private EditText et_pbone;
        private Button bt_show;
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
            bt_show.setOnClickListener(this);
        }
        public void init(){
            et_pbone= (EditText) findViewById(R.id.et_phone);
            bt_show= (Button) findViewById(R.id.btn_show);
            textView=(TextView)findViewById(R.id.tv_show);
        }
        @Override
        public void onClick(View v) {
            String num = et_pbone.getText().toString();
            textView.setText("333您的手机号是"+num);
        }
    
    }

    4.匿名类----注意更改:<activity android:name=".SActivity">

    package inspur.com.hello;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    /**
     * Created by Administrator on 2018/5/4.
     */
    public class FoActivity extends Activity {
        private EditText et_pbone;
        private Button bt_show;
        private TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //初始化
            et_pbone= (EditText) findViewById(R.id.et_phone);
            bt_show= (Button) findViewById(R.id.btn_show);
            textView=(TextView)findViewById(R.id.tv_show);
    
            //事件源绑定事件监听器
            bt_show.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    String num = et_pbone.getText().toString();
                    textView.setText("444您的手机号是"+num);
                }
    
            });
        }
    }

     5.通过doClick--新建了layout.xml

    (1)新建了five_layout.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">
        <EditText
            android:id="@+id/et_phone"
            android:hint="请输入手机号"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:onClick="doClick"
            android:id="@+id/btn_show1"
            android:text="显示您输入的号码"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:onClick="doClick"
            android:id="@+id/btn_show2"
            android:text="显示您输入的号码"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

    (2).FiAcvicity.java

    package inspur.com.button;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    /**
     * Created by Administrator on 2018/5/4.
     */
    public class FiActivity extends Activity {
        private EditText editText;
        private Button button;
        private TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.five_layout);
            init();
    
    
    
        }
        public  void  init(){
            editText = (EditText) findViewById(R.id.et_phone);
            textView = (TextView) findViewById(R.id.tv_show);
        }
        public void doClick(View v){
            switch (v.getId()){
                case R.id.btn_show1:
                    String num1 = editText.getText().toString();
                    textView.setText(num1+"hehehe");
                    break;
                case R.id.btn_show2:
                    String num2 = editText.getText().toString();
                    textView.setText(num2+"haha");
                    break;
            }
        }
    }

    (3)AndroidMainifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="inspur.com.button">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".FiActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    xaml的margin和css的margin对比
    easyui datagrid连续删除问题
    面试常用的代码片段
    JS实现table表格在鼠标移动出现一行变色或者十字叉变色
    Ubuntu 18.04 LTS 64位Linux搭建Kubernetes 1.15.3并join子节点的完整过程
    Java定义队结构,实现入队、出队操作
    Java定义栈结构,实现入栈、出栈操作
    Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历
    MySQL计算两个日期相差的天数、月数、年数
    Nginx(web服务器)与Tomcat(应用服务器)搭建集群
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/8990564.html
Copyright © 2011-2022 走看看