جلسه نهم اندروید بخش یک Intent
چهارشنبه, ۱۳ فروردين ۱۳۹۹، ۰۱:۳۷ ق.ظ
two.java:
package ir.ben.hassan; import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.DragStartHelper; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; public class two extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); TextView tv= findViewById(R.id.tv2);//alt+2enter to cast Toast.makeText(this, "hey", Toast.LENGTH_SHORT).show(); Bundle extras=getIntent().getExtras(); if(extras!=null) { Toast.makeText(this, "extra", Toast.LENGTH_SHORT).show(); String email=""; String phone=""; String name=""; if(extras.containsKey("email")) email=extras.getString("email"); if(extras.containsKey("phone")){ phone=extras.getString("phone"); } if(extras.containsKey("name")) name=extras.getString("name"); tv.setText("name: "+name+"\n"); tv.append("email: "+email+"\n"); tv.append("phone: "+phone+"\n"); } else Toast.makeText(this, "no extra", Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem itemok= menu.add("Confirm"); itemok.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); itemok.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Intent intent=new Intent();//////just inverse of the other intent intent.putExtra("messge","yoohoooo"); setResult(RESULT_OK,intent); //or instead of the red lines could be just: setResult(RESULT_OK); no need to make intent finish();//finishes this activity which is no longer needed return false; } }); return super.onCreateOptionsMenu(menu); } }
کلیک راست->generate getter
formifo.java
package ir.ben.hassan; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; public class FormInfo implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { private Activity activity; private LinearLayout forminfo; private Context context; private EditText inputname; private EditText inputphone; private EditText inputemail; private CheckBox cbshare; private Button btsub; public FormInfo(Activity activity,int layoutId) { this.activity=activity; forminfo=activity.findViewById(layoutId);//here the instructor did a cast (LinearLayout) this.activity=activity; init(); } private void init() { if(forminfo==null) return; inputname=forminfo.findViewById(R.id.inputname);//here the instructor did a cast (EditText) inputphone=forminfo.findViewById(R.id.inputphone); inputemail=forminfo.findViewById(R.id.inputemail); cbshare=forminfo.findViewById(R.id.cbshare); btsub=forminfo.findViewById(R.id.btsub); btsub.setOnClickListener(this); cbshare.setOnCheckedChangeListener(this); } //این یاسی ها با گتر ساخته شدن public EditText getInputname() { return inputname; } public EditText getInputphone() { return inputphone; } public EditText getInputemail() { return inputemail; } public CheckBox getCbshare() { return cbshare; } public Button getBtsub() { return btsub; } @Override public void onClick(View v) { if(v.getId()==btsub.getId()) { String email=inputemail.getText().toString().trim(); String phone=inputphone.getText().toString().trim(); String name=inputname.getText().toString().trim(); if(isvalidinput(name,phone,email)) { Toast.makeText(activity, "Valid Input", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(activity,two.class); intent.putExtra("name",name); intent.putExtra("email",email); if(cbshare.isChecked()) intent.putExtra("phone",phone); activity.startActivity(intent); } } } //این قبلا پرایوت بود به خاطر گترها پابلیک شد public boolean isvalidinput(String name,String phone,String email) { if(name==null) name=inputname.getText().toString().trim(); if(email==null) email=inputemail.getText().toString().trim(); if(phone==null) phone=inputphone.getText().toString().trim(); if (name.length() < 3) { Toast.makeText(activity, "Name should be at least 3 characters", Toast.LENGTH_SHORT).show(); inputname.requestFocus(); return false; } if (cbshare.isChecked() && !phone.isEmpty() && (phone.length() != 11 || !phone.startsWith("09"))) { Toast.makeText(activity, "Wrong mobile number", Toast.LENGTH_SHORT).show(); inputphone.requestFocus(); return false; } if (!email.contains("@") || (email.lastIndexOf('.') < email.indexOf('@')) || email.split("@").length>2) { Toast.makeText(activity, "Wrong email address", Toast.LENGTH_SHORT).show(); inputemail.requestFocus(); return false; } return true; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.getId()==cbshare.getId()) { inputphone.setEnabled(isChecked); } } }
main activity java
package ir.ben.hassan; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.Date; import java.util.Random; public class MainActivity extends AppCompatActivity { public static final int reqcod=24; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final FormInfo form=new FormInfo(this,R.id.forminfo); form.getBtsub().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=form.getInputname().getText().toString().trim(); String phone=form.getInputphone().getText().toString().trim(); String email=form.getInputemail().getText().toString().trim(); if(form.isvalidinput(name,phone,email)) { Intent intent= new Intent(MainActivity.this,two.class); intent.putExtra("name",name); intent.putExtra("email",email); if(form.getCbshare().isChecked()) intent.putExtra("phone",phone); startActivityForResult(intent,reqcod); } } }); // form.getInputname().setTextColor(0xFFFF7002); // form.getInputname().setTextColor(Color.argb(200,5240,67,4)); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==reqcod) { if(resultCode==RESULT_OK) { Toast.makeText(this, data.getStringExtra("messge"), Toast.LENGTH_SHORT).show(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main,menu); return super.onCreateOptionsMenu(menu); } } }
باگ برا من اگه شماره تلفن وارد نکنه ولی زده باشه share اپ بسته میشه اگه اون دستور null ها رو بذارم درست میشه؟
نع نشد
دلیلش این بوده:
if(extras.containsKey("phone")){ phone=extras.getString("phone").substring(2);
کد استاد اون substring رو برداشته بوده
میان بر: وقتی new می نویسی کنترل اسپس بزن . (منو پیدا کن من یه میان بر هستم که توش اینتر و alt enter نداره!)
new ctrl space
itemok.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
۹۹/۰۱/۱۳