I am trying to make a a phonepad in android and I need to know how I make my backspace button work when I press it, it remove my last number and so on.
b13.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int len=text.length();
text.setText(text.getText().toString());
String res=text.substring(0,len-1);
}
});
b13 is button id for remove button
You're on the right track. Try this:
#Override
public void onClick(View view) {
String newText = myTextView.getText().toString()
.substring(0, myTextView.getText.toString.length()-1);
myTextView.setText(newText);
}
Take a look at KeyEvent, there is a constant called KEYCODE_BACK that should achieve this behavior
Related
i do for this code but not supported for ths issue and what i do for solution this like as if codition inside any listener.
if (btn.isEnabled()) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
But why???
any button's onClickListener will only be called if its "enabled".
You dont need to bother about onClickListeners which are assigned to disabledButtons.
suppose your button is disabled at the launch of activity then this listener wont be applied to your button.
Now after some time if you enable this button (may be after some event etc.)
"THEN ALSO THIS LISTENER WONT WORK" as you did not set the listener at the first place...
so IMO dont put that in if...
this code successfully.
if (button.isEnabled()) {
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Test", 10).show();
}
});
}
I have a little problem with my app. In my app dynamically I create a table of EditText, obtaining an array of EditText, at this point would like to draw, when I click on the EditText my Keyboard instead of that of default. How do I handle the click of an array of EditText?
Try :
for(int i=0; i<etArray.lenght; i++) {
etArray[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
Can anyone please tell why the if statement never gets executed even though I enter ran in the edit text field and press ok? When the user enters ran and presses the button ok, I want another button to become visible which enables him to stop the current activity. Basically I want to know why the if is skipped.
public class Reciever extends Activity{
protected static final String TAG = null;
private Button ok,stp;
private TextView tv;
private EditText ev;
private String s1,s2,s3,s4;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
s1="nar";
setContentView(R.layout.stop);
tv=(TextView) findViewById(R.id.textView1);
tv.setText(s1);
ev=(EditText) findViewById(R.id.editText1);
ok=(Button) findViewById(R.id.ok_button);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
s2="ran";
tv.setText(ev.getText().toString());
s3=ev.getText().toString();
if(s3==s2)//not going inside this loop
{
stp=(Button) findViewById(R.id.stopb);
stp.setVisibility(View.VISIBLE);
stp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
});
}
}
if(s3==s2)//no
just replace the above line with below
if(s3.equalsIgnoreCase(s2))//no
Use .equals instead of == to compare the value of two strings.
if (s2.equals(s3))
Using == tests for reference equality. Two strings can contain the same characters but have different references.
You can't compare text with ==, you need to use equals.
if(s3.equals(s2))
I don't know how to send a backspace key event to a EditText from my own button. Here is what i tried:
Button backSpace=(Button)findViewById(R.id.backSpace_tab);
backSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.dispatchKeyEvent(new KeyEvent(KeyEvent.KEYCODE_DEL,KeyEvent.KEYCODE_P));
}
});
From the Android developer docs:
public KeyEvent (int action, int code)
Create a new key event.
Parameters
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code
The first parameter should be an action code. In your case you should use ACTION_DOWN, because you want to simulate a keypress:
public static final int ACTION_DOWN
getAction() value: the key has been pressed down.
So this should work:
#Override
public void onClick(View v)
{
text.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}
imgvw_back.setOnClickListener(this);
imgvw.setOnClickListener(this);
static id=10
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.back:
Log.v("back",""+id--);
break;
case R.id.forward:
Log.v("next",""+id++);
break;
}
}
i am using this kind of concept but mostly fire R.id.back part ,what can i do plz give solution for this problem...
The id may not be what you think it is, you could do something like this.
imgvw_back.setOnClickListener(this);
imgvw.setOnClickListener(this);
static id=10
#Override
public void onClick(View v)
{
if(v == imgvw_back)
{
Log.v("back",""+id--);
}
else if(v == imgvw)
{
Log.v("next",""+id++);
}
}
Use inline onClickListeners for each button.
imgvw_back.setOnClickListener(new onClickListener(){
#Override
public void onClick(View v){
Log.v("back", "")
}
});
Is imgvw your forward button? Just wondering, cause your back button is imgvw_back, would assume forward would be named accordingly imgvw_forward.
You're probably missing missing to set the clickListener to the forward button as well.