正在尝试从片段重定向到活动



今天我试图从一个片段中打开一个新的活动。

我的目标是,链接到片段的XML上的按钮将我重定向到允许创建客户端的"活动"。

这是我的片段-->客户端

package com.madera.kotlin.Controller.Home.ui.chantier
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.madera.kotlin.R

class ClientFragment : Fragment() {

private lateinit var clientViewModel: ClientViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
clientViewModel =
ViewModelProvider(this).get(ClientViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_client, container, false)
//region Components
val btnCreateClient: Button = root.findViewById(R.id.btnCreateClient)
val textView: TextView = root.findViewById(R.id.text_client)
clientViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
}

这是我的HomeViewActivity,它使用导航控制器:

package com.madera.kotlin.Controller.Home
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.navigation.NavigationView
import com.google.android.material.snackbar.Snackbar
import com.madera.kotlin.Controller.Home.ui.client.NewClientActivity
import com.madera.kotlin.R

class HomeViewActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_view)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Ajouter une action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_project, R.id.nav_slideshow
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
//region Components
val btnCreateClient = findViewById(R.id.btnCreateClient) as? Button
btnCreateClient?.setOnClickListener {
// Le code a exécuté quand l'utilisateur à cliquer sur le bouton
Toast.makeText(
this@HomeViewActivity,
"Redirection vers l'espace projet !",
Toast.LENGTH_SHORT
).show()
val i = Intent(this, NewClientActivity::class.java)
startActivity(i)
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.home_view, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Controller.Home.ui.chantier.ClientFragment">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="929dp"
android:layout_height="168dp"
android:background="#00AD59"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/txtUsername"
android:layout_width="365dp"
android:layout_height="65dp"
android:layout_marginStart="10px"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:layout_marginEnd="10px"
android:layout_marginRight="10px"
android:layout_marginBottom="10px"
android:text="Client View"
android:textColor="#FFFFFF"
android:textSize="36sp" />
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15px"
android:layout_marginLeft="15px"
android:layout_marginTop="150px"
android:layout_marginEnd="10px"
android:layout_marginRight="10px"
android:background="#FFFFFF" />
<Button
android:name="com.madera.kotlin.Controller.Home.ui.chantier.ClientFragment"
android:id="@+id/btnCreateClient"
android:layout_width="415dp"
android:layout_height="61dp"
android:layout_gravity="center"
android:layout_marginStart="1100px"
android:layout_marginLeft="1100px"
android:text="Créer un nouveau client"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_client"
android:layout_width="0dp"
android:layout_height="0dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/relativeLayout"
app:layout_constraintVertical_bias="0.0"
tools:listitem="@layout/recyclerview_client" />
<TextView
android:id="@+id/text_client"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我想在点击btnCreateClient时启动的活动,它会将用户重定向到一个表单页面:

package com.madera.kotlin.Controller.Home.ui.client
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.widget.*
import com.madera.kotlin.R
class NewClientActivity : AppCompatActivity() {
private lateinit var ClientNameView: EditText
private lateinit var ClientSecteurView: EditText
private lateinit var ClientAdresseView: EditText
private lateinit var ClientVilleView: EditText
private lateinit var ClientCodePostalView: EditText
private lateinit var ClientDescriptionView: EditText
private lateinit var ClientIsProView: CheckBox
@Override
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.new_client_view)
ClientNameView = findViewById(R.id.txtName)
ClientSecteurView = findViewById(R.id.txtSecteur)
ClientAdresseView = findViewById(R.id.txtAdress)
ClientVilleView = findViewById(R.id.txtCity)
ClientCodePostalView = findViewById(R.id.txtPostal)
ClientDescriptionView = findViewById(R.id.txtDesc)
ClientIsProView = findViewById(R.id.checkProfessionnel)

val button = findViewById<Button>(R.id.button_save)
button.setOnClickListener {
val replyIntent = Intent()
if (TextUtils.isEmpty(ClientNameView.text) && TextUtils.isEmpty(ClientAdresseView.text)){
setResult(Activity.RESULT_CANCELED, replyIntent)
}else{
// TODO : Envoi de l'ensbemble des informations nécessaire à la création client
val clientName = ClientNameView.text.toString()
val clientAdress = ClientAdresseView.text.toString()
val clientSecteur = ClientSecteurView.text.toString()
val clientVille = ClientVilleView.text.toString()
val clientCodePostal = ClientCodePostalView.text.toString()
val clientDescription = ClientDescriptionView.text.toString()
val clientIsPro = ClientIsProView.isChecked.toString()
replyIntent.putExtra(EXTRA_CLIENT_NAME, clientName)
replyIntent.putExtra(EXTRA_CLIENT_ADRESS, clientAdress)
replyIntent.putExtra(EXTRA_CLIENT_SECTEUR, clientSecteur)
replyIntent.putExtra(EXTRA_CLIENT_VILLE, clientVille)
replyIntent.putExtra(EXTRA_CLIENT_CODEPOSTAL, clientCodePostal)
replyIntent.putExtra(EXTRA_CLIENT_ISPRO, clientIsPro)
replyIntent.putExtra(EXTRA_CLIENT_DESCRIPTION, clientDescription)
setResult(Activity.RESULT_OK, replyIntent)
}
finish()
}

}
companion object{
const val EXTRA_CLIENT_NAME = "nom"
const val EXTRA_CLIENT_ADRESS = "adresse"
const val EXTRA_CLIENT_SECTEUR = "secteur"
const val EXTRA_CLIENT_VILLE = "ville"
const val EXTRA_CLIENT_CODEPOSTAL = "codePostal"
const val EXTRA_CLIENT_ISPRO = "isPro"
const val EXTRA_CLIENT_DESCRIPTION = "description"
}
}

我试图将setOnLickListener添加到我的客户端Fragment中,但我无法使其工作。

你知道怎么做吗?

如果事先感谢你的帮助,我真的很感激。

梅尔文·卡雷雷

您的问题来自于尝试将ClientFragment用作IntentContext

活动继承自Context,但片段没有,因此必须使用Fragment.getContext()检索它。

相关内容

  • 没有找到相关文章

最新更新