三种方式实现点击按钮显示输入的信息
Posted sunshine_96
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三种方式实现点击按钮显示输入的信息相关的知识,希望对你有一定的参考价值。
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.内部类
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); } }
以上是关于三种方式实现点击按钮显示输入的信息的主要内容,如果未能解决你的问题,请参考以下文章