赞
踩
利用基于监听的事件处理方式实现简单文本编辑器,分别实现字体颜色、大小样式变化以及文本重命名的四种功能,这四种功能要求分别用内部类、外部类、类自身和匿名内部类四种形式实现,其中重点掌握匿名内部类的使用。
MainActivity:
package com.example.shijianchuli; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.graphics.Color; import android.graphics.Typeface; 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 Button red,green,blue; private TextView testText; private Button bigger,smaller; private Button bold,italic,moren; private boolean isItalic=false,isBold=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); testText = (TextView) findViewById(R.id.testTest); red = (Button) findViewById(R.id.red); green = (Button) findViewById(R.id.green); blue = (Button) findViewById(R.id.blue); ColorListner myColorLister = new ColorListner(); red.setOnClickListener(myColorLister); green.setOnClickListener(myColorLister); blue.setOnClickListener(myColorLister); testText.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请输入新的内容"); builder.setIcon(R.mipmap.ic_launcher); final EditText contentText = new EditText(MainActivity.this); builder.setView(contentText); builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { testText.setText(contentText.getText().toString()); } }); builder.setNegativeButton("取消", null); builder.create().show(); return false;} }); bigger=(Button)findViewById(R.id.bigger); smaller=(Button)findViewById(R.id.smaller); SizeListener mysizeListener=new SizeListener(testText); bigger.setOnClickListener(mysizeListener); smaller.setOnClickListener(mysizeListener); testText.setTypeface(Typeface.DEFAULT); bold=(Button)findViewById(R.id.bold); italic=(Button)findViewById(R.id.italic); moren=(Button)findViewById(R.id.moren); italic.setOnClickListener(this); bold.setOnClickListener(this); moren.setOnClickListener(this); } private class ColorListner implements View.OnClickListener{ public void onClick(View v){ switch (v.getId()){ case R.id.red: testText.setTextColor(Color.RED);break; case R.id.blue: testText.setTextColor(Color.BLUE);break; case R.id.green: testText.setTextColor(Color.GREEN);break; default:break; } } } public void onClick(View v){ switch (v.getId()){ case R.id.italic: isItalic=!isItalic; break; case R.id.bold: isBold=!isBold; break; case R.id.moren: isItalic=false; isBold=false; break; default: break; } if(isItalic){ if(isBold){ testText.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC); }else { testText.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC); } }else { if(isBold){ testText.setTypeface(Typeface.MONOSPACE,Typeface.BOLD); }else { testText.setTypeface(Typeface.MONOSPACE,Typeface.NORMAL); } } } }
SizeListener:
package com.example.shijianchuli; import android.util.TypedValue; import android.view.View; import android.widget.TextView; public class SizeListener implements View.OnClickListener{ private TextView tv; public SizeListener(TextView tv){ this.tv=tv; } public void onClick(View v){ float f=tv.getTextSize(); switch (v.getId()){ case R.id.bigger: f=f+4; break; case R.id.smaller: f=f-4; break; default: break;} if(f<=8){ f=8;} tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,f); } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/testTest" android:gravity="center" android:text="@string/test_text" android:padding="10dp" android:textSize="20sp"/> <TableRow android:layout_marginLeft="10dp"> <TextView android:text="@string/size"/> <Button android:id="@+id/bigger" android:text="@string/bigger"/> <Button android:id="@+id/smaller" android:text="@string/smaller"/> </TableRow> <TableRow android:layout_marginLeft="10dp"> <TextView android:text="@string/color"/> <Button android:id="@+id/red" android:text="@string/red"/> <Button android:id="@+id/green" android:text="@string/green"/> <Button android:id="@+id/blue" android:text="@string/blue"/> </TableRow> <TableRow android:layout_marginLeft="10dp"> <TextView android:text="@string/style"/> <Button android:id="@+id/bold" android:text="@string/bold"/> <Button android:id="@+id/italic" android:text="@string/italic"/> <Button android:id="@+id/moren" android:text="@string/moren"/> </TableRow> </TableLayout>
strings.xml:
<resources> <string name="app_name">文本编辑器</string> <string name="hello_world">Hello world</string> <string name="menu_settings">Settings</string> <string name="color">颜色</string> <string name="red">红色</string> <string name="green">绿色</string> <string name="blue">蓝色</string> <string name="size">大小</string> <string name="bigger">增大</string> <string name="smaller">变小</string> <string name="style">样式</string> <string name="bold">加粗</string> <string name="italic">倾斜</string> <string name="content">内容</string> <string name="test_text">我是计算机应用班邓家帅</string> <string name="moren">默认</string> </resources>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。