Created
September 21, 2017 14:58
-
-
Save khatthaphone/74187d5998ce2f32aa011d14c0b01f67 to your computer and use it in GitHub Desktop.
Stupid Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.sengx.intentdemo; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
public class MainActivity extends Activity implements View.OnClickListener { | |
Button btnNext; | |
Button btnDial; | |
EditText edtDialNum; | |
TextView tvResult; | |
// 1. ສ້າງໂຕ request code | |
public static final int REQUEST_CODE = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
btnNext = (Button) findViewById(R.id.btnNext); | |
btnNext.setOnClickListener(this); | |
btnDial = (Button) findViewById(R.id.btnDial); | |
btnDial.setOnClickListener(this); | |
edtDialNum = (EditText) findViewById(R.id.edtDialNum); | |
tvResult = (TextView) findViewById(R.id.tvResult); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode){ | |
// case(R.layout.activity_second): | |
// 4. ກວດຄືນໂຕ request code ແມ່ນໂຕດຽວກັນບໍ່ | |
case REQUEST_CODE: | |
// 5. ເອົາໂຕນີ້ອອກຈຶ່ວໄດ້, ສົງໄສ resultCode ບໍ່ກົງກັນ | |
// if(resultCode == Activity.RESULT_OK){ | |
String resultValue = data.getStringExtra("resultAnimal"); | |
tvResult.setText(resultValue); | |
// } | |
break; | |
default: | |
break; | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
int WidgetID = v.getId(); | |
switch(WidgetID){ | |
case R.id.btnNext: | |
Intent intent = new Intent(getApplicationContext(),SecondActivity.class); | |
startActivity(intent); | |
break; | |
case R.id.btnDial: | |
String number = edtDialNum.getText().toString().trim(); | |
Intent call = new Intent(getApplicationContext(), SecondActivity.class); | |
call.putExtra("message", number); | |
// startActivity(call); | |
// 2. ໃຊ້ startActivityForResult ແທນມ ໃສ່ REQUEST ເຂົ້າໄປນຳ | |
startActivityForResult(call, REQUEST_CODE); | |
// String tel = edtDialNum.getText().toString().trim(); | |
// Intent intentDial = new Intent(Intent.ACTION_DIAL); | |
// intentDial.setData(Uri.parse("tel:"+tel)); | |
// startActivity(intentDial); | |
break; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.sengx.intentdemo; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.Toast; | |
public class SecondActivity extends Activity implements View.OnClickListener { | |
Button btnBack; | |
RadioGroup rdgAnimal; | |
RadioButton rdAnimal; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_second); | |
btnBack = (Button) findViewById(R.id.btnBack); | |
btnBack.setOnClickListener(this); | |
rdgAnimal = (RadioGroup)findViewById(R.id.rdgAnimal); | |
Bundle bundle = getIntent().getExtras(); | |
Toast.makeText(this, bundle.getString("message").toString(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onClick(View v) { | |
int WidgetID = v.getId(); | |
switch (WidgetID){ | |
case R.id.btnBack: | |
int selectedId = rdgAnimal.getCheckedRadioButtonId(); | |
rdAnimal = (RadioButton) findViewById(selectedId); | |
Toast.makeText(this, rdAnimal.getText().toString(), Toast.LENGTH_SHORT).show(); | |
Intent resultIntent = new Intent(); | |
resultIntent.putExtra("resultAnimal", rdAnimal.getText().toString()); | |
// setResult(Activity.RESULT_OK, resultIntent); | |
// 3. setResult ພ້ອມໃສ່ request code ກັບຄືນ | |
setResult(MainActivity.REQUEST_CODE, resultIntent); | |
finish(); | |
break; | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment