Cross Button in EditText Android
Cross Button in EditText Android for deleting clearing text in EditText Example source code:
Example allow delete text in EditText by cross button, or do Button click performance.
main.xml type your package name and class
Put into drawable folder cross and ok image.
CustomEditText.java
YourActivity.java
//android.okhelp.cz/wiktionary-aplikace-pro-android/
Example allow delete text in EditText by cross button, or do Button click performance.
main.xml type your package name and class
Put into drawable folder cross and ok image.
<cz.okhelp.wiktionary.CustomEditText
android:id="@+id/editTextZadejSlovo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="TypeAndPressGreen"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:drawableLeft="@drawable/cross"
android:drawableRight="@drawable/ok" />
<!--button is invisible 0 height 0 width for performance click on button in EditText-->
<Button
android:id="@+id/btnGO"
android:layout_width="0sp"
android:layout_height="0sp"
android:layout_weight="0"
android:text="GO" />
CustomEditText.java
package cz.okhelp.wiktionary; // your package name
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.EditText;
public class CustomEditText extends EditText
{
private Drawable dLeft,dRight;
private Rect lBounds,rBounds;
private static Button btnOk;
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top,
Drawable right, Drawable bottom)
{
if(left !=null) {
dLeft = left;
}
if(right !=null){
dRight = right;
}
super.setCompoundDrawables(left, top, right, bottom);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
btnOk.requestFocus();
btnOk.performClick();
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
final int x = (int)event.getX();
final int y = (int)event.getY();
if(event.getAction() == MotionEvent.ACTION_UP && dLeft!=null) {
lBounds = dLeft.getBounds();
int n1 = this.getLeft();
int n2 = this.getLeft()+lBounds.width();
int n3 = this.getPaddingTop();
int n4 = this.getHeight()-this.getPaddingBottom();
// leva strana
if( x>=(this.getLeft())
&& x<=(this.getLeft()+lBounds.width())
&& y>=this.getPaddingTop()
&& y<=(this.getHeight()-this.getPaddingBottom()))
{
this.setText("");
event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
}
}
if(event.getAction() == MotionEvent.ACTION_UP && dRight!=null)
{
rBounds = dRight.getBounds();
int n1 = this.getRight()-rBounds.width();
int n2 = this.getRight()-this.getPaddingRight();
int n3 = this.getPaddingTop();
int n4 = this.getHeight()-this.getPaddingBottom();
// prava strana
if(x>=(this.getRight()-rBounds.width()) && x<=(this.getRight()-this.getPaddingRight())
&& y>=this.getPaddingTop() && y<=(this.getHeight()-this.getPaddingBottom()))
{
btnOk.requestFocus();
btnOk.performClick();
event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
}
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable
{
dRight = null;
rBounds = null;
super.finalize();
}
public void setBtnOk(Button btnOk) {
this.btnOk = btnOk;
}
public Button getBtnOk() {
return btnOk;
}
}
YourActivity.java
//onCreate
Button mBtnGO = (Button)findViewById(R.id.btnGO);
CustomEditText mEditZadani = (CustomEditText)this.findViewById(R.id.editTextZadejSlovo);
mEditZadani.setBtnOk(mBtnGO);
mBtnGO.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff for signInButtonClick
}
});
//android.okhelp.cz/wiktionary-aplikace-pro-android/
396LW NO topic_id
AD
Další témata ....(Topics)
LayoutLib is too recent. Update your tool!
Eclipse Android Graphical layout resolving problem.
Eclipse Android Graphical layout resolving problem.
- Open in Eclipse menu Help ->Check for Updates
- Select updates:
- Press Next and update all
- Restart Eclipse
[caption id="attachment_596" align="alignleft" width="300" caption="Restart Eclipse if updates finished."][/caption]
public boolean isConnected() {
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("isConnected", e.getMessage());
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
return false;
}
Brand | Samsung |
Model (codename) | Galaxy Mini (S5570) |
Price (cena, včetně DPH) | 3500 / 06.2012 |
Display size in Inch (v palcích) | 3.14 |
Display-resolution | 240x320 |
Dotek-typ | kapacitní |
CPU typ | MSM7227 |
CPU MHz | 600 |
CPU core | |
L2 cache | yes |
RAM | 256 |
ROM | 512 |
GPU | Adreno 200 |
NenaMark2 Benchmark | |
GPU-GLBenchmark | |
Baterie mAh | 1200 |
Foto MPx | 3 |
Autofocus | no |
Video | QVGA (320 x 240) při 15 frames/s |
Official Android ICS | Android Froyo 2.2 |
CyanogenMod support | yes |
Dotek-prstů-max | Dual-touch (two fingers) |
Display-ppi | 127 |
Display-retina | 39% |
Networks | GSM&EDGE (850 / 900 / 1.800 / 1.900 MHz) 3G (900 / 2.100 MHz) |
Connectivity | Bluetooth V2.1 , USB V2.0 , USB mass storage , SyncML(DM) , WIFI , AGPS, 3.5 mm jack |
Note |
Samsung S5570 Galaxy Mini - image
(Our software)
1 | Samsung Galaxy S2 | 13.8% (1,232) |
2 | Samsung Galaxy S | 8.5% (762) |
3 | Samsung Galaxy Tab | 7.4% (659) |
4 | Samsung Galaxy Mini | 5.4% (483) |
5 | Samsung Galaxy S | 4.0% (356) |
6 | HTC Desire HD | 3.8% (341) |
7 | Samsung Galaxy Ace | 3.8% (339) |
8 | HTC Wildfire | 3.2% (286) |
9 | Samsung Galaxy Fit | 3.1% (274) |
10 | SEMC Xperia X10 | 2.1% (190) |
TimerTask with updating of TextView here
//android.okhelp.cz/asynctask-example-android-with-progressbar/
//android.okhelp.cz/timer-task-timertask-run-cancel-android-example/
package cz.okhelp.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TimerActivity extends Activity {
TextView hTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
// public void schedule (TimerTask task, long delay, long period)
// Schedule a task for repeated fixed-delay execution after a specific delay.
//
// Parameters
// task the task to schedule.
// delay amount of time in milliseconds before first execution.
// period amount of time in milliseconds between subsequent executions.
myTimer.schedule(myTask, 3000, 1500);
}
class MyTimerTask extends TimerTask {
public void run() {
// ERROR
hTextView.setText("Impossible");
// how update TextView in link below
// //android.okhelp.cz/timer-task-timertask-run-cancel-android-example/
System.out.println("");
}
}
}
//android.okhelp.cz/asynctask-example-android-with-progressbar/
//android.okhelp.cz/timer-task-timertask-run-cancel-android-example/
Editace: 2013-12-09 13:02:09
Počet článků v kategorii: 396
Url:cross-button-in-edittext-android