(一)内部类
1,布局
<?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:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="点击显示"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
/></LinearLayout>
2,逻辑处理
package helloworld.com.inspur.demo6;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;import org.w3c.dom.Text;public class MainActivity extends AppCompatActivity {
private EditText et;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et);
tv=(TextView)findViewById(R.id.tv);
et.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_A:
tv.setText("AAAAAA");
break;
case KeyEvent.KEYCODE_B:
tv.setText("BBB");
break;
case KeyEvent.KEYCODE_C:
tv.setText("CCC");
break;
case KeyEvent.KEYCODE_D:
tv.setText("DDD");
break;
}
return false;
}
});
}
}
(二)外部类
1,布局同上
<?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:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="点击显示"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
/></LinearLayout>
2,逻辑处理
package helloworld.com.inspur.app2;
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 ed;
private Button but;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
but=(Button)findViewById(R.id.but);
tv_show=(TextView)findViewById(R.id.tv_show);
but.setOnClickListener(new DoClick(ed,tv_show)); }}
class DoClick implements View.OnClickListener{
private EditText ed;
private TextView tv_show; public DoClick(EditText ed, TextView tv_show) {
this.ed = ed;
this.tv_show = tv_show;
} @Override
public void onClick(View v) {
String str=ed.getText().toString();
tv_show.setText(str);
}
}
3,相对比于内部类的处理方式
不可以使用MainActivity中的成员变量,需要通过带参数的构造函数的方式将两个参数对应起来。
(三)匿名内部类
1,布局同上
2,逻辑代码处理
package helloworld.com.inspur.app2;
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 ed;
private Button but;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
but=(Button)findViewById(R.id.but);
tv_show=(TextView)findViewById(R.id.tv_show);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=ed.getText().toString();
tv_show.setText(str);
}
}); }}
(四)本类实现
package helloworld.com.inspur.app2;
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 implements View.OnClickListener{
private EditText ed;
private Button but;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
but=(Button)findViewById(R.id.but);
tv_show=(TextView)findViewById(R.id.tv_show);
but.setOnClickListener(this); } @Override
public void onClick(View v) {
String str=ed.getText().toString();
tv_show.setText(str);
}
}
(五)组建绑定事件
1,布局
<?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:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:onClick="doClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="点击显示"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
/></LinearLayout>
2,逻辑代码的实现
package helloworld.com.inspur.app2;
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 ed;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
tv_show=(TextView)findViewById(R.id.tv_show);
}
public void doClick(View v)
{
String str=ed.getText().toString();
tv_show.setText(str);
}
}