如何在Android中将TextView的文本转换为位图?

问题描述 投票:4回答:2

我是Android开发的初学者,正在尝试开发一个程序,用户可以在该程序中转换在TextView中显示的文本((TextView显示了我从资产导入的代码39条码字体文本)] >在按下“转换为位图!”后,将其转换为位图。按钮。我曾尝试在Google周围搜索,但只设法获得了将字符串转换为位图的答案,而没有关于在哪里键入代码的指南,因此我对此感到困惑。我尝试使用谷歌搜索后尝试输入的代码来运行该程序,但每次按下转换按钮时,它都会崩溃。

真的希望您能提供帮助!先感谢您! :D

以下是我到目前为止的代码:-

* 根据Simon的代码进行编辑

在java:

public class MainActivity extends Activity 
implements OnClickListener {
    //Called when activity is first created

    TextView tv1;
    ImageView iv;
    Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv1 = (TextView) findViewById(R.id.txtV);  
        tv1.setDrawingCacheEnabled(true); 

        //To change to code 39 barCode font
        Typeface barcodefont = Typeface.createFromAsset(getAssets(),                 
                "fonts/IDAutomationHC39M_FREE.otf");         
        TextView tv = (TextView) findViewById(R.id.txtV);         
        tv.setTypeface(barcodefont);
    }

    public void onClick(View v) {

        tv1.buildDrawingCache(); 
        iv.setImageBitmap(tv1.getDrawingCache()); 
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Logcat错误:

10-04 06:33:25.076: E/AndroidRuntime(1423): FATAL EXCEPTION: main
10-04 06:33:25.076: E/AndroidRuntime(1423): java.lang.IllegalStateException: Could not find a method ConvertText(View) in the activity class com.example.txtvbitmapconverter.MainActivity for onClick handler on view class android.widget.Button with id 'btnConvert'
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$1.onClick(View.java:3578)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View.performClick(View.java:4084)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$PerformClick.run(View.java:16966)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Handler.handleCallback(Handler.java:615)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.os.Looper.loop(Looper.java:137)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.reflect.Method.invoke(Method.java:511)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at dalvik.system.NativeStart.main(Native Method)
10-04 06:33:25.076: E/AndroidRuntime(1423): Caused by: java.lang.NoSuchMethodException: ConvertText [class android.view.View]
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at java.lang.Class.getMethod(Class.java:915)
10-04 06:33:25.076: E/AndroidRuntime(1423):     at android.view.View$1.onClick(View.java:3571)
10-04 06:33:25.076: E/AndroidRuntime(1423):     ... 11 more

我是Android开发的初学者,正在尝试开发一个程序,用户可以在该程序中转换在TextView中显示的文本(TextView正在显示代码39条码字体文本,我......]]

android string button bitmap textview
2个回答
8
投票
尝试一下

添加到您的onCreate()


0
投票
onCreate()中的行:

TextView tv1 = (TextView) findViewById(R.id.txtV);

© www.soinside.com 2019 - 2024. All rights reserved.