大家好,我做了一个抽屉活动,但是在我的片段中,我想要一个按钮这样当我按下按钮时,这应该将文本更改为Hi或Hello,但是当我尝试添加代码时,我不知道代码应该出现在哪里在FirstFragemt.kt文件中它抛出一个错误,请帮帮我谢谢
这是我的主要活动:
class MainActivity : AppCompatActivity(),FirstFragment.OnFragmentInteractionListener,SecondFragment.OnFragmentInteractionListener, NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show()
}
val toggle = ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.first_activity -> {
title = "Fragment One"
val fragment = FirstFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentID, fragment, "FragmentOne") //create first framelayout with id fram in the activity where fragments will be displayed
fragmentTransaction.commit() // Handle the camera action
}
R.id.second_activity -> {
title = "Fragment Second"
val fragment = SecondFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentID, fragment, "FragmentSecond") //create first framelayout with id fram in the activity where fragments will be displayed
fragmentTransaction.commit()
}
R.id.nav_share -> {
}
R.id.nav_exit -> {
System.exit(0)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
override fun onFragmentInteraction(uri: Uri) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
这是My FirstFragent文件:
`
class FirstFragment : Fragment() {
private var mParam1: String? = null
private var mParam2: String? = null
private var mListener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments != null) {
mParam1 = arguments.getString(ARG_PARAM1)
mParam2 = arguments.getString(ARG_PARAM2)
}
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_first, container, false)
}
// TODO: Rename method, update argument and hook method into UI event
fun onButtonPressed(uri: Uri) {
if (mListener != null) {
mListener!!.onFragmentInteraction(uri)
}
}
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
mListener = context
} else {
throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
mListener = null
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
*
* See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
*/
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}
companion object {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private val ARG_PARAM1 = "param1"
private val ARG_PARAM2 = "param2"
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FirstFragment.
*/
// TODO: Rename and change types and number of parameters
fun newInstance(param1: String, param2: String): FirstFragment {
val fragment = FirstFragment()
val args = Bundle()
args.putString(ARG_PARAM1, param1)
args.putString(ARG_PARAM2, param2)
fragment.arguments = args
return fragment
}
}
} //必需的空公共构造函数
这是我的FirstFragment布局xml文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.a3.aakap.ftrial.FirstFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<Button
android:id="@+id/button"
android:layout_width="200sp"
android:layout_height="50sp"
android:text="Click me !!"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_gravity="center"/>
</FrameLayout>
答案 0 :(得分:2)
在那些Fragment中创建onViewCreated的overover这里是一个代码:
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.setOnClickListener{
text.setText("Hello")
}
}
答案 1 :(得分:1)
所以,你所缺少的是抓住你的片段的视图,以及它里面的视图(主要是你的按钮和你的TextView),并在它们上面做些什么。
在你的FirstFragment.kt:
private lateinit var mTextView: TextView
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater!!.inflate(R.layout.fragment_first, container, false)
mTextView = findViewById(R.id.text)
findViewById<Button>(R.id.button).setOnClickListener { mTextView.text = "Hi" }
return view
}
答案 2 :(得分:0)
class FirstFragment : Fragment() {
private var ctx: Context? = null
private var self: View? = null
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
ctx = container?.context
self = LayoutInflater.from(ctx).inflate(R.layout.fragment_start, container, false)
val bDaButton = self?.findViewById<Button>(R.id.bDaButton)
bDaButton?.setOnClickListener {
Toast.makeText(ctx, "button works!", Toast.LENGTH_SHORT).show()
}
return self
}
}
<Button
android:id="@+id/bDaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Work!"/>