【问题标题】:Can't Get Focus on Button in FragmentActivity无法专注于 FragmentActivity 中的按钮
【发布时间】:2015-03-26 01:17:28
【问题描述】:

花了足够的时间没有成功后,我将其发布在这里寻求帮助。我有一个从以下 FragmentActivity 调用的 ListFragment。在布局中,顶部有一个提交按钮,用户可以滚动列表并在填写完信息后按下提交按钮。但是我不能点击这个提交按钮。它永远不会得到焦点。我该如何解决?

感谢您的帮助。

我有这个 FragmentActivity:

public class SurveyFragmentAnchor extends FragmentActivity {

Button submit; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_survey_fragment);
    setTitle("Feedback and Survey");

    submit = (Button) findViewById(R.id.submit_button);

    submit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             Log.v("SURVEY", "Button pressed");
        }
    });
}
}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="4dp" >

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#e5e5e5">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#ee000000" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:paddingBottom="10dp"
            android:text="Rating (1-5) 5= Excellent, 4=Very good, 3=Good,\n2= Satisfactory, 1= Needs Improvement"
            android:textColor="#FFF" />

            <Button
                android:background="@drawable/button_blue2"
                android:textColor="#FFF"
                android:id="@+id/submit_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/my_survey_fragment"
                android:text="Submit" />


   </TableRow>
</TableLayout>      

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:drawSelectorOnTop="false" >
    </ListView>
</LinearLayout>

<fragment
    android:name="com.survey.MySurvey"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="47dp"
    android:id="@+id/my_survey_fragment">
</fragment>

【问题讨论】:

  • 根据您提到的答案,为什么我将 Button 代码放在 Fragment 中,它得到了“提交”的值,这意味着它不为空,但我仍然无法单击它。问题是该按钮永远不会成为焦点,因此无法单击。
  • 它必须在不同的视图上。

标签: android button focus android-fragmentactivity android-listfragment


【解决方案1】:

尝试在按钮上使用这些字段:

android:focusable="true"
android:focusableInTouchMode="true" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多