Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.kt
- import android.app.Activity
- import android.content.Intent
- import android.net.Uri
- import android.net.UrlQuerySanitizer
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import android.widget.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- }
- //I. Набиране на определен телефон
- fun btn_phone(view: View) {
- //1 Извикване на конретен телефон
- /*val phone="0881002003"
- val obaj= Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone))
- startActivity(obaj)*/
- //2 Извикване на номера,въведен в EditText
- val nomer=findViewById<EditText>(R.id.editTextPhone).text.toString()
- if( nomer.isEmpty() || nomer.length!=10 )
- Toast.makeText(this, "Incorrect phone number", Toast.LENGTH_SHORT).show()
- else {
- val obaj = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + nomer))
- startActivity(obaj)
- }
- }
- fun go_http(view: View) {
- //1
- /*val web=findViewById<TextView>(R.id.textView2).text.toString()
- val adres=Intent(Intent.ACTION_VIEW,Uri.parse(web))
- startActivity(adres)*/
- //2
- /*val obj=findViewById<TextView>(view.id)
- val web=obj.text.toString()
- val adres=Intent(Intent.ACTION_VIEW,Uri.parse(web))
- startActivity(adres)*/
- //3
- startActivity(Intent(Intent.ACTION_VIEW,Uri.parse(findViewById<TextView>(view.id).text.toString())))
- }
- fun btn_pictures(view: View) {
- // Галерия със снимки
- val intnt=Intent(Intent.ACTION_PICK)
- intnt.type="image/*"
- startActivity(intnt)
- }
- fun btn_show(view: View) {
- //val sec_act=Intent(this,SecondActivity::class.java)
- //startActivity(sec_act)
- val r_group=findViewById<RadioGroup>(R.id.radioGroup)
- if( r_group.checkedRadioButtonId==-1)
- Toast.makeText(this, "Изберете вид любимец", Toast.LENGTH_SHORT).show()
- else {
- val vid=findViewById<RadioButton>(r_group.checkedRadioButtonId).text.toString()
- //val tv=findViewById<TextView>(R.id.textView)
- //tv.text=vid
- val sec_act=Intent(this,SecondActivity::class.java)
- sec_act.putExtra("Animal",vid)
- startActivityForResult(sec_act,0)
- }
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if(resultCode==Activity.RESULT_OK) {
- val r_group=findViewById<RadioGroup>(R.id.radioGroup)
- val animal=findViewById<RadioButton>(r_group.checkedRadioButtonId).text.toString()
- val imeto= data?.getStringExtra("Ime")
- val tv = findViewById<TextView>(R.id.textView)
- tv.text = "Името на " + animal + " e " + imeto
- }
- else
- Toast.makeText(this, "Няма име", Toast.LENGTH_SHORT).show()
- }
- }
- Activity_main.xml
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="27dp"
- android:layout_marginEnd="26dp"
- android:text="Hello World!"
- app:layout_constraintEnd_toEndOf="@+id/editTextPhone"
- app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
- <EditText
- android:id="@+id/editTextPhone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginTop="4dp"
- android:ems="10"
- android:inputType="phone"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="1dp"
- android:onClick="btn_phone"
- android:text="Call"
- app:layout_constraintEnd_toEndOf="@+id/button3"
- app:layout_constraintStart_toStartOf="@+id/button3"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="17dp"
- android:onClick="go_http"
- android:text="http://www.abv.bg"
- android:textAllCaps="false"
- android:textColor="#1534DD"
- android:textSize="20sp"
- android:textStyle="bold"
- app:layout_constraintBottom_toTopOf="@+id/textView3"
- app:layout_constraintStart_toStartOf="@+id/textView3" />
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="31dp"
- android:layout_marginTop="188dp"
- android:onClick="go_http"
- android:text="http://www.google.com"
- android:textAllCaps="false"
- android:textColor="#1534DD"
- android:textSize="20sp"
- android:textStyle="bold"
- app:layout_constraintStart_toStartOf="@+id/button2"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="100dp"
- android:layout_marginTop="34dp"
- android:onClick="btn_pictures"
- android:text="Pictures"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/textView3" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="59dp"
- android:layout_marginTop="2dp"
- android:layout_marginEnd="61dp"
- android:onClick="btn_show"
- android:text="Go"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/radioGroup"
- app:layout_constraintTop_toTopOf="@+id/radioGroup" />
- <RadioGroup
- android:id="@+id/radioGroup"
- android:layout_width="145dp"
- android:layout_height="209dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.218"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.745">
- <RadioButton
- android:id="@+id/radioButton5"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Котка" />
- <RadioButton
- android:id="@+id/radioButton4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Куче"
- tools:layout_editor_absoluteX="58dp"
- tools:layout_editor_absoluteY="437dp" />
- <RadioButton
- android:id="@+id/radioButton6"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Животно"
- tools:layout_editor_absoluteX="58dp"
- tools:layout_editor_absoluteY="485dp" />
- </RadioGroup>
- SecondActivity.kt
- import android.app.Activity
- import android.content.Intent
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.view.View
- import android.widget.EditText
- import android.widget.TextView
- class SecondActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_second)
- val anim=intent.getStringExtra("Animal")
- val tv=findViewById<TextView>(R.id.textView4)
- tv.text="Името на "+anim
- }
- fun btn_go(view:View) {
- val ime=findViewById<EditText>(R.id.editTextTextPersonName)
- val sec_intent=Intent()
- if( !ime.text.isEmpty() ) {
- sec_intent.putExtra("Ime", ime.text.toString())
- setResult(Activity.RESULT_OK, sec_intent)
- finish()
- }
- else {
- setResult(Activity.RESULT_CANCELED, sec_intent)
- finish()
- }
- }
- }
- Activity_Second.xml
- <TextView
- android:id="@+id/textView4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="89dp"
- android:layout_marginTop="86dp"
- android:text="TextView"
- android:textSize="20sp"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/editTextTextPersonName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="83dp"
- android:layout_marginTop="21dp"
- android:ems="10"
- android:inputType="textPersonName"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/textView4" />
- <Button
- android:id="@+id/button4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="6dp"
- android:layout_marginTop="20dp"
- android:onClick="btn_go"
- android:text="Ok"
- app:layout_constraintStart_toStartOf="@+id/editTextTextPersonName"
- app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement