2014-01-24 7 views
0

Я разрабатываю приложение с помощью sms, мое требование: 1) отправлять sms со статическими значениями 2) всякий раз, когда мы нажимаем кнопку редактирования, мы можем изменять значения и отправлять sms, я могу успешно их выполнить но мое третье требование заключается в отправке смс только при нажатии на редактирование и изменении значений. Нажмите кнопки отправки, без изменения значения. Я не хочу отправлять смс. Я хочу, когда вы нажмете на кнопку редактирования, а затем отправьте sms, я попытался с if (editBtn.isSelected), но он не работает. пожалуйста скажите любое решениекак отправить sms в android

мой код

XML файл

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:orientation="vertical" > 

    <Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editBtn" 
     android:text="EDIT" 
     android:textColor="#00FF00" 
     android:onClick="editListener"/> 
    <LinearLayout android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
    <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="NUMBER" 
      android:textSize="20dip"/> 
    <EditText android:layout_width="150dip" 
      android:layout_height="wrap_content" 
      android:id="@+id/numberEdit" 
      android:text="8989897979" 
      android:clickable="false" 
      android:cursorVisible="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      /> 
    </LinearLayout> 
    <LinearLayout android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
    <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="MESSAGE" 
      android:textSize="20dip"/> 
    <EditText android:layout_width="150dip" 
      android:layout_height="wrap_content" 
      android:id="@+id/messageEdit" 
      android:text="HAI HOW R U" 
      android:clickable="false" 
      android:cursorVisible="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      /> 

    </LinearLayout> 
    <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/subBtn" 
      android:text="SUBMIT"/> 
    </LinearLayout> 

Activty

public class MainActivity extends Activity { 

private EditText contactEdit; 
private EditText messageEdit; 
private Button submitBtn; 
private Button editBtn; 

String contact; 
String message; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    contactEdit = (EditText)findViewById(R.id.numberEdit); 
    messageEdit = (EditText)findViewById(R.id.messageEdit); 
    submitBtn = (Button)findViewById(R.id.subBtn); 
    editBtn  = (Button)findViewById(R.id.editBtn); 

    if(editBtn.isSelected()){ 
    submitBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      contact = contactEdit.getText().toString(); 
      message = messageEdit.getText().toString(); 

      try{ 
       SmsManager manger = SmsManager.getDefault(); 

       manger.sendTextMessage(contact, null, message, null, 
            null); 
       Toast.makeText(getApplicationContext(), "SMS SENT", 
           100).show(); 
      } 
      catch(Exception e){ 

       Toast.makeText(getApplicationContext(), "SMS NOT   
          SEND", 100).show(); 
       e.printStackTrace(); 

      } 

     } 
    }); 
    } 
    else { 
     Toast.makeText(getApplicationContext(), "NOT EDITED", 100).show(); 
    } 
    editBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      changeEdit(); 

     } 
    }); 
} 


private void changeEdit(){ 
    contactEdit.setClickable(true); 
    contactEdit.setCursorVisible(true); 
    contactEdit.setFocusable(true); 
    contactEdit.setFocusableInTouchMode(true); 
    messageEdit.setClickable(true); 
    messageEdit.setCursorVisible(true); 
    messageEdit.setFocusable(true); 
    messageEdit.setFocusableInTouchMode(true); 
} 

} 
+0

вы можете взять переменную флаг, чтобы проверить, нажата ли редактировать или нет. –

+0

, и вы можете сохранить значения, чтобы позже убедиться в * отправке *, если значения действительно были изменены или нет. – Zeeshan

+0

Я новичок в android, я не знаю, как взять fag variableplease сказать мне – Durga

ответ

2

Попробуйте использовать OnTouchListener для EditText

образца,

editBtn.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 

          changeEdit(); 

      return true; 
     } 
    }); 

, если ее не работает

Создать логическую переменную Глобально и сделать это верно, если и дотронулся на EditText,

// В grobal

boolean e_clicked = false; 

// Внутри EditText onTouchListener

e_clicked=true; 

и проверка

if(e_clicked) 
{ 
// your sms send action 
} 
+0

Он упомянул, «без изменений, я не хочу отправлять msg». Что делать, если я прикасаюсь к кнопке * Редактировать * и не изменяю значения? Значения должны быть сохранены для более поздней проверки, я думаю. – Zeeshan

+0

также не работает – Durga

+0

@ Zeeshan, если это правда, добавьте TextWatcher Listener для Edittext –

1

попробовать это

 public class AlarmReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Intent intent=new Intent(context,Service.class); 
    //get lat/lng and pass it or get from service if u want 
    intent.putExtra("lat","lat"); 
    intent.putExtra("lon","lng"); 
    context.startService(intent); 

} 
    } 

    public void SetAlarm(Context context) { 

     AlarmManager am = (AlarmManager) context 
       .getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, AlarmReceiver.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     Calendar calendar = Calendar.getInstance(); 

      calendar.setTimeInMillis(System.currentTimeMillis()); 

      calendar.add(Calendar.MINUTE, 10); 
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 
       calendar.getTimeInMillis(), pi); 
    } 
Смежные вопросы