Android基于监听的事件处理4种方式实现简单文本编辑器

效果图:
在这里插入图片描述

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">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是XXX"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_margin="30dp"
        />
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大小:"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

        <Button
            android:id="@+id/bt11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增大"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

        <Button
            android:id="@+id/bt12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减小"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

    </LinearLayout>

    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色:"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

        <Button
            android:id="@+id/bt21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="红色"
            android:textSize="20sp"
            android:layout_margin="10dp"/>
        <Button
            android:id="@+id/bt22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绿色"
            android:textSize="20sp"
            android:layout_margin="10dp"/>
        <Button
            android:id="@+id/bt23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

    </LinearLayout>

    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="样式:"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

        <Button
            android:id="@+id/bt31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加粗"
            android:textSize="20sp"
            android:layout_margin="10dp"/>
        <Button
            android:id="@+id/bt32"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="倾斜"
            android:textSize="20sp"
            android:layout_margin="10dp"/>
        <Button
            android:id="@+id/bt33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="默认"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内容:"
            android:textSize="20sp"
            android:layout_margin="10dp"/>

        <EditText
            android:id="@+id/et"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:maxLines="1"
            />
    </LinearLayout>
    <Button
        android:id="@+id/bt41"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存文本"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:layout_margin="10dp"/>
</LinearLayout>

MainActivity部分:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //字体大小范围
    private int[] SizeList={10,20,30};
    //初始字体大小
    int SizeStar=1;
    private TextView tv;
    private EditText et;
    private Button bt11,bt12,bt21,bt22,bt23,bt31,bt32,bt33,bt41;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test5_1);
        tv=(TextView)findViewById(R.id.title);//文本

        //内部类( 字体大小)
        //第一排按钮
        bt11=(Button)findViewById(R.id.bt11) ;
        bt12=(Button)findViewById(R.id.bt12) ;
            //创建内部类实现
        bt11.setOnClickListener(new Bt11());
        bt12.setOnClickListener(new Bt12());

        //外部类(字体颜色)
        //第二排按钮
        bt21=(Button)findViewById(R.id.bt21);
        bt22=(Button)findViewById(R.id.bt22);
        bt23=(Button)findViewById(R.id.bt23);
        //注册监听器
        bt21.setOnClickListener(new Button2Listener(tv));
        bt22.setOnClickListener(new Button2Listener(tv));
        bt23.setOnClickListener(new Button2Listener(tv));

        //类自身(字体样式)
        //第三排按钮
        bt31=(Button)findViewById(R.id.bt31);
        bt32=(Button)findViewById(R.id.bt32);
        bt33=(Button)findViewById(R.id.bt33);
        //绑定
        bt31.setOnClickListener(this);
        bt32.setOnClickListener(this);
        bt33.setOnClickListener(this);

        //匿名内部类(文本内容)
        //输入框和保存按钮
        bt41=(Button) findViewById(R.id.bt41);//保存文本按钮
        et=(EditText) findViewById(R.id.et);//文本输入框

        bt41.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText(et.getText());
                Log.i("log","匿名内部类");
            }
        });

    }



    //创建内部类实现
    class Bt11 implements View.OnClickListener {//内部类创建
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {//重写onClick()
                SizeStar++;
                tv.setTextSize(SizeList[SizeStar%3]);
            }
        }
    class Bt12 implements View.OnClickListener {//内部类创建
        @Override
        public void onClick(View v) {//重写onClick()
            SizeStar--;
            tv.setTextSize(SizeList[SizeStar%3]);

        }
    }

//类自身(字体样式)
//实现view.OnClickListener接口后重写方法
@Override
public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt31://加粗
                        tv.setTypeface(null, Typeface.BOLD);//加粗
                break;
            case R.id.bt32://倾斜
                         tv.setTypeface(null, Typeface.ITALIC);//倾斜
                break;
            case R.id.bt33://默认
                        tv.setTypeface(null,Typeface.NORMAL);

                break;
        }
}

外部类Button2Listener:

public class Button2Listener implements View.OnClickListener {
    private TextView tv;
    //构造方法传入文本框tv
    public Button2Listener(TextView tv){
        this.tv=tv;
    }
    //重写onClick方法
    @Override
    public void onClick(View v) {
        //区分不同颜色按钮的点击
        switch (v.getId()){
            case R.id.bt21:
                tv.setTextColor(Color.RED);
                Log.i("log","红色");
                break;
            case R.id.bt22:
                tv.setTextColor(Color.GREEN);
                Log.i("log","绿色");
                break;
            case R.id.bt23:
                tv.setTextColor(Color.BLUE);
                Log.i("log","蓝色");
                break;
            default:break;

        }
    }
}

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值