相关疑难解决方法(0)

如何使用片段中的XML onClick处理按钮单击

Pre-Honeycomb(Android 3),每个Activity都已注册,可通过onClickLayout的XML中的标签处理按钮点击:

android:onClick="myClickMethod"
Run Code Online (Sandbox Code Playgroud)

在该方法中,您可以使用view.getId()和switch语句来执行按钮逻辑.

随着Honeycomb的推出,我将这些活动分解为碎片,可以在许多不同的活动中重复使用.按钮的大部分行为都是与Activity无关的,我希望代码驻留在Fragments文件中,而不使用OnClickListener为每个按钮注册的旧(pre 1.6)方法.

final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});
Run Code Online (Sandbox Code Playgroud)

问题是,当我的布局被夸大时,它仍然是接收按钮点击的托管活动,而不是单个碎片.对两者都有好的方法

  • 注册片段以接收按钮点击?
  • 将Click中的click事件传递给它们所属的片段?

xml android button android-fragments

425
推荐指数
8
解决办法
24万
查看次数

onClick内部片段调用Activity

我现在正在将内容封装成一个片段,并遇到一个难以谷歌的问题.在我的片段里面是一些带有onClick属性的按钮,但它们在Activity上调用而不是来自android系统的片段 - 这使得封装有点笨拙.有没有办法让onClick的反射内容调用片段?我现在看到的唯一解决方案是不使用xml中的onClick并通过代码在片段内设置click-listeners.

android android-fragments

44
推荐指数
3
解决办法
5万
查看次数

按钮监听器在android中的片段中的按钮

我是Android新手,并试图自己学习.但我正在与Fragments度过艰难时期.我正在创建一个简单的应用程序来学习片段.我觉得这看起来很傻但我真的无法让它发挥作用.

我想要做的只是点击Fragment_One中的按钮(buttonSayHi),Fragment_One应该被Fragment_Two取代.

我不确定何时调用Fragment代码以及我应该在哪里编写代码来调用第二个片段.我收到错误:无法启动Activity组件.

但是,如果我丢弃按钮的监听器并且片段显示在活动中,则代码可以正常工作.

我做了大量的研究,阅读developer.android.com上的片段教程以及Lars Vogella的教程.我认为我的概念并不清楚.

任何帮助将不胜感激.

以下是代码:

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayoutFragmentContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editTextPersonName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/buttonSayHi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Say Hi" 
        android:onClick="onButtonClicked"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="I will say Hi!" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

package com.example.fragmenttutorial;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

32
推荐指数
2
解决办法
14万
查看次数

Android片段中的按钮单击不起作用

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment1"
    android:name="sithi.test.fragmenttest.Fragment1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="btnClick1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ActivityMain.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu); …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments

2
推荐指数
1
解决办法
8567
查看次数

onClick方法仅适用于MainActivity

我有一个ImageButton,我想在我的应用程序的某个Fragment类中使用它.但是当我在我的片段类中使用它时,应用程序崩溃了.但我可以在我的MainActivity中使用它而不会崩溃.我怎样才能解决这个问题?

这是我在我的片段类中使用的代码

public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(activity, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.song_popup, popup.getMenu());
        popup.show();

    }
Run Code Online (Sandbox Code Playgroud)

这会导致应用程序崩溃.但是当我在MainActivity中使用它时(在mainactivity中,"activity"被this.getApplicationContext替换)它可以工作.

我的按钮xml

<ImageButton
                android:id="@+id/popUp"
                android:layout_width="20dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="2dp"
                android:background="@null"
                android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
                android:onClick="showPopup" />
Run Code Online (Sandbox Code Playgroud)

logcat的

java.lang.IllegalStateException: Could not find a method showPopup(View) in the activity class com.fm.etunes.phone.MainActivity for onClick handler on view class android.widget.ImageButton with id 'popUp'
            at android.view.View$1.onClick(View.java:3814)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at …
Run Code Online (Sandbox Code Playgroud)

android illegalstateexception android-imagebutton

-5
推荐指数
1
解决办法
858
查看次数