Android ui--redraw the EditText and implement the fade color of the button

Source: Internet
Author: User

What is implemented in this article is a more general redraw of EditText and adding gradients to buttons or windows.

Since EditText is inherited from TextView, it is possible to redraw the edittext, and only inherit EditText and rewrite its OnDraw () method when redrawing.

When adding gradients to a button or window, you need to borrow the Gradientdrawable method to set the direction of the gradient, the color of the gradient, place the gradient's color in an array, and then access it. and use the Setbackgrounddrawable () method to display it on the interface.

The operation of this example:

The specific implementation code is as follows:

Mainactivity

 PackageCom.example.testxml;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;Importandroid.graphics.drawable.GradientDrawable;Importandroid.graphics.drawable.GradientDrawable.Orientation;ImportAndroid.os.Bundle;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {PrivateButton btn; PrivateEditText edtxt;//private Drawedit drawedtxt;@SuppressLint ("Wrongcall") @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); BTN= (Button) This. Findviewbyid (R.id.btncenter); //set top-down gradient for buttonsGradientdrawable gradientdrawable =NewGradientdrawable (Orientation.left_right,New int[] {color.red,color.black,color.yellow}); //sets the gradient background color of the current window//GetWindow (). setbackgrounddrawable (gradientdrawable);btn.setbackgrounddrawable (gradientdrawable); }}

Redraw the EditText Drawedit code as follows

1  PackageCom.example.testxml;2 3 ImportAndroid.content.Context;4 ImportAndroid.graphics.Canvas;5 ImportAndroid.graphics.Color;6 ImportAndroid.graphics.Paint;7 ImportAndroid.util.AttributeSet;8 ImportAndroid.widget.EditText;9 Ten  Public classDraweditextendsedittext{ One  A     //implementing the Drawedit constructor -      PublicDrawedit (Context context) { -         Super(context); the     } -  -      PublicDrawedit (Context context, AttributeSet attrs) { -         Super(context, attrs); +     } -  +     protected voidOnDraw (canvas canvas) { A         Super. OnDraw (canvas); atPaint paint =NewPaint (); -Paint.settextsize (18); - Paint.setcolor (color.green); -         //Draw Text -Canvas.drawtext ("Drawing Text", 2, GetHeight ()/2 + 5, paint); -          in     } -  to  +}

XML layout file:

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:id= "@+id/linearlayout1"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6android:orientation= "Vertical"7Tools:context= ". Mainactivity ">8     9<ButtonTenAndroid:id= "@+id/btncenter" OneAndroid:layout_width= "Fill_parent" Aandroid:layout_height= "Wrap_content" -android:layout_margintop= "20DP" -android:text= "@string/btncenter"/> the  -<!--to customize the edittext when it is redrawn so that no calls are made in mainactivity, and he will call-- -          -<Com.example.testxml.DrawEdit +Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" +android:paddingleft= "100DP" A/> at      -  -</LinearLayout>

The process of redrawing the edittext and the gradient of the settings for the button is achieved.

Android ui--redraw the EditText and implement the fade color of the button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.