Advertisement
deyanivanov966

Упражнение 6. Използване на Intent

Jun 2nd, 2022
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 9.44 KB | None | 0 0
  1. MainActivity.kt
  2.  
  3. import android.app.Activity
  4. import android.content.Intent
  5. import android.net.Uri
  6. import android.net.UrlQuerySanitizer
  7. import androidx.appcompat.app.AppCompatActivity
  8. import android.os.Bundle
  9. import android.view.View
  10. import android.widget.*
  11.  
  12. class MainActivity : AppCompatActivity() {
  13.     override fun onCreate(savedInstanceState: Bundle?) {
  14.         super.onCreate(savedInstanceState)
  15.         setContentView(R.layout.activity_main)
  16.     }
  17.  
  18.     //I. Набиране на определен телефон
  19.     fun btn_phone(view: View) {
  20.         //1 Извикване на конретен телефон
  21.         /*val phone="0881002003"
  22.         val obaj= Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone))
  23.         startActivity(obaj)*/
  24.         //2 Извикване на номера,въведен в EditText
  25.         val nomer=findViewById<EditText>(R.id.editTextPhone).text.toString()
  26.         if( nomer.isEmpty() || nomer.length!=10 )
  27.             Toast.makeText(this, "Incorrect phone number", Toast.LENGTH_SHORT).show()
  28.         else {
  29.             val obaj = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + nomer))
  30.             startActivity(obaj)
  31.         }
  32.     }
  33.  
  34.     fun go_http(view: View) {
  35.         //1
  36.         /*val web=findViewById<TextView>(R.id.textView2).text.toString()
  37.         val adres=Intent(Intent.ACTION_VIEW,Uri.parse(web))
  38.         startActivity(adres)*/
  39.         //2
  40.         /*val obj=findViewById<TextView>(view.id)
  41.         val web=obj.text.toString()
  42.         val adres=Intent(Intent.ACTION_VIEW,Uri.parse(web))
  43.         startActivity(adres)*/
  44.         //3
  45.         startActivity(Intent(Intent.ACTION_VIEW,Uri.parse(findViewById<TextView>(view.id).text.toString())))
  46.     }
  47.  
  48.     fun btn_pictures(view: View) {
  49.         // Галерия със снимки
  50.         val intnt=Intent(Intent.ACTION_PICK)
  51.         intnt.type="image/*"
  52.         startActivity(intnt)
  53.     }
  54.  
  55.     fun btn_show(view: View) {
  56.         //val sec_act=Intent(this,SecondActivity::class.java)
  57.         //startActivity(sec_act)
  58.         val r_group=findViewById<RadioGroup>(R.id.radioGroup)
  59.         if( r_group.checkedRadioButtonId==-1)
  60.             Toast.makeText(this, "Изберете вид любимец", Toast.LENGTH_SHORT).show()
  61.         else {
  62.             val vid=findViewById<RadioButton>(r_group.checkedRadioButtonId).text.toString()
  63.             //val tv=findViewById<TextView>(R.id.textView)
  64.             //tv.text=vid
  65.             val sec_act=Intent(this,SecondActivity::class.java)
  66.             sec_act.putExtra("Animal",vid)
  67.             startActivityForResult(sec_act,0)
  68.         }
  69.     }
  70.  
  71.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  72.         super.onActivityResult(requestCode, resultCode, data)
  73.         if(resultCode==Activity.RESULT_OK) {
  74.             val r_group=findViewById<RadioGroup>(R.id.radioGroup)
  75.             val animal=findViewById<RadioButton>(r_group.checkedRadioButtonId).text.toString()
  76.             val imeto= data?.getStringExtra("Ime")
  77.             val tv = findViewById<TextView>(R.id.textView)
  78.             tv.text = "Името на " + animal + " e " + imeto
  79.         }
  80.         else
  81.             Toast.makeText(this, "Няма име", Toast.LENGTH_SHORT).show()
  82.     }
  83. }
  84.  
  85. Activity_main.xml
  86.  
  87. <TextView
  88.     android:id="@+id/textView"
  89.     android:layout_width="wrap_content"
  90.     android:layout_height="wrap_content"
  91.     android:layout_marginTop="27dp"
  92.     android:layout_marginEnd="26dp"
  93.     android:text="Hello World!"
  94.     app:layout_constraintEnd_toEndOf="@+id/editTextPhone"
  95.     app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
  96.  
  97. <EditText
  98.     android:id="@+id/editTextPhone"
  99.     android:layout_width="wrap_content"
  100.     android:layout_height="wrap_content"
  101.     android:layout_marginStart="24dp"
  102.     android:layout_marginTop="4dp"
  103.     android:ems="10"
  104.     android:inputType="phone"
  105.     app:layout_constraintStart_toStartOf="parent"
  106.     app:layout_constraintTop_toTopOf="parent" />
  107.  
  108. <Button
  109.     android:id="@+id/button"
  110.     android:layout_width="wrap_content"
  111.     android:layout_height="wrap_content"
  112.     android:layout_marginTop="1dp"
  113.     android:onClick="btn_phone"
  114.     android:text="Call"
  115.     app:layout_constraintEnd_toEndOf="@+id/button3"
  116.     app:layout_constraintStart_toStartOf="@+id/button3"
  117.     app:layout_constraintTop_toTopOf="parent" />
  118.  
  119. <TextView
  120.     android:id="@+id/textView2"
  121.     android:layout_width="wrap_content"
  122.     android:layout_height="wrap_content"
  123.     android:layout_marginBottom="17dp"
  124.     android:onClick="go_http"
  125.     android:text="http://www.abv.bg"
  126.     android:textAllCaps="false"
  127.     android:textColor="#1534DD"
  128.     android:textSize="20sp"
  129.     android:textStyle="bold"
  130.     app:layout_constraintBottom_toTopOf="@+id/textView3"
  131.     app:layout_constraintStart_toStartOf="@+id/textView3" />
  132.  
  133. <TextView
  134.     android:id="@+id/textView3"
  135.     android:layout_width="wrap_content"
  136.     android:layout_height="wrap_content"
  137.     android:layout_marginStart="31dp"
  138.     android:layout_marginTop="188dp"
  139.     android:onClick="go_http"
  140.     android:text="http://www.google.com"
  141.     android:textAllCaps="false"
  142.     android:textColor="#1534DD"
  143.     android:textSize="20sp"
  144.     android:textStyle="bold"
  145.     app:layout_constraintStart_toStartOf="@+id/button2"
  146.     app:layout_constraintTop_toTopOf="parent" />
  147.  
  148. <Button
  149.     android:id="@+id/button2"
  150.     android:layout_width="wrap_content"
  151.     android:layout_height="wrap_content"
  152.     android:layout_marginStart="100dp"
  153.     android:layout_marginTop="34dp"
  154.     android:onClick="btn_pictures"
  155.     android:text="Pictures"
  156.     app:layout_constraintStart_toStartOf="parent"
  157.     app:layout_constraintTop_toBottomOf="@+id/textView3" />
  158.  
  159. <Button
  160.     android:id="@+id/button3"
  161.     android:layout_width="wrap_content"
  162.     android:layout_height="wrap_content"
  163.     android:layout_marginStart="59dp"
  164.     android:layout_marginTop="2dp"
  165.     android:layout_marginEnd="61dp"
  166.     android:onClick="btn_show"
  167.     android:text="Go"
  168.     app:layout_constraintEnd_toEndOf="parent"
  169.     app:layout_constraintStart_toEndOf="@+id/radioGroup"
  170.     app:layout_constraintTop_toTopOf="@+id/radioGroup" />
  171.  
  172. <RadioGroup
  173.     android:id="@+id/radioGroup"
  174.     android:layout_width="145dp"
  175.     android:layout_height="209dp"
  176.     app:layout_constraintBottom_toBottomOf="parent"
  177.     app:layout_constraintEnd_toEndOf="parent"
  178.     app:layout_constraintHorizontal_bias="0.218"
  179.     app:layout_constraintStart_toStartOf="parent"
  180.     app:layout_constraintTop_toTopOf="parent"
  181.     app:layout_constraintVertical_bias="0.745">
  182.  
  183.     <RadioButton
  184.         android:id="@+id/radioButton5"
  185.         android:layout_width="match_parent"
  186.         android:layout_height="wrap_content"
  187.         android:text="Котка" />
  188.  
  189.     <RadioButton
  190.         android:id="@+id/radioButton4"
  191.         android:layout_width="match_parent"
  192.         android:layout_height="wrap_content"
  193.         android:text="Куче"
  194.         tools:layout_editor_absoluteX="58dp"
  195.         tools:layout_editor_absoluteY="437dp" />
  196.  
  197.     <RadioButton
  198.         android:id="@+id/radioButton6"
  199.         android:layout_width="match_parent"
  200.         android:layout_height="wrap_content"
  201.         android:text="Животно"
  202.         tools:layout_editor_absoluteX="58dp"
  203.         tools:layout_editor_absoluteY="485dp" />
  204. </RadioGroup>
  205.  
  206. SecondActivity.kt
  207.  
  208.  
  209. import android.app.Activity
  210. import android.content.Intent
  211. import androidx.appcompat.app.AppCompatActivity
  212. import android.os.Bundle
  213. import android.view.View
  214. import android.widget.EditText
  215. import android.widget.TextView
  216.  
  217. class SecondActivity : AppCompatActivity() {
  218.     override fun onCreate(savedInstanceState: Bundle?) {
  219.         super.onCreate(savedInstanceState)
  220.         setContentView(R.layout.activity_second)
  221.  
  222.         val anim=intent.getStringExtra("Animal")
  223.         val tv=findViewById<TextView>(R.id.textView4)
  224.         tv.text="Името на "+anim
  225.     }
  226.  
  227.     fun btn_go(view:View) {
  228.         val ime=findViewById<EditText>(R.id.editTextTextPersonName)
  229.         val sec_intent=Intent()
  230.  
  231.         if( !ime.text.isEmpty() ) {
  232.             sec_intent.putExtra("Ime", ime.text.toString())
  233.             setResult(Activity.RESULT_OK, sec_intent)
  234.             finish()
  235.         }
  236.         else {
  237.             setResult(Activity.RESULT_CANCELED, sec_intent)
  238.             finish()
  239.         }
  240.     }
  241. }
  242. Activity_Second.xml
  243. <TextView
  244.     android:id="@+id/textView4"
  245.     android:layout_width="wrap_content"
  246.     android:layout_height="wrap_content"
  247.     android:layout_marginStart="89dp"
  248.     android:layout_marginTop="86dp"
  249.     android:text="TextView"
  250.     android:textSize="20sp"
  251.     app:layout_constraintStart_toStartOf="parent"
  252.     app:layout_constraintTop_toTopOf="parent" />
  253.  
  254. <EditText
  255.     android:id="@+id/editTextTextPersonName"
  256.     android:layout_width="wrap_content"
  257.     android:layout_height="wrap_content"
  258.     android:layout_marginStart="83dp"
  259.     android:layout_marginTop="21dp"
  260.     android:ems="10"
  261.     android:inputType="textPersonName"
  262.     app:layout_constraintStart_toStartOf="parent"
  263.     app:layout_constraintTop_toBottomOf="@+id/textView4" />
  264.  
  265. <Button
  266.     android:id="@+id/button4"
  267.     android:layout_width="wrap_content"
  268.     android:layout_height="wrap_content"
  269.     android:layout_marginStart="6dp"
  270.     android:layout_marginTop="20dp"
  271.     android:onClick="btn_go"
  272.     android:text="Ok"
  273.     app:layout_constraintStart_toStartOf="@+id/editTextTextPersonName"
  274.     app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement