【问题标题】:TextField focusability in a ListViewListView 中的 TextField 可聚焦性
【发布时间】:2012-10-22 13:49:32
【问题描述】:

我是 android 新手,想要制作一个自定义适配器,以便每一行都包含一个图像,并在其旁边放置一些信息。我还想放置一个 textField。我这样做了,但我不能使用它们,意思是它们看起来应该是这样,但是当我触摸它时它永远不会打开软键盘。我的猜测是它没有得到请求的焦点。有什么想法吗?我会发布我写了这么多的 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="?android:attr/listPreferredItemHeight"
    android:padding="6dip"
    android:descendantFocusability="blocksDescendants" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/std" />

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


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padding_small"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name" />



    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:text="TextView"
        android:textSize="10dp" />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padding_small"
    android:orientation="horizontal" >



    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Grade" />





<EditText
    android:id="@+id/txtPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword" >

</EditText>

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padding_small"
    android:orientation="horizontal" >



    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Comment" />










    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:textSize="10dp" />

</LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/padding_small"
            android:orientation="horizontal" >



    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Present" />


    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp" />

</LinearLayout>

这就是我的 getView 方法

    public View getView(final int position, View convertView, ViewGroup parent) {


    convertView=null;


    ViewHolder holder;

    if (convertView == null) {

        convertView = mInflater.inflate(com.example.myapp.R.layout.rw, null);           

        TextView tt = (TextView) convertView.findViewById(mIds[1]);
        TextView bt = (TextView) convertView.findViewById(mIds[0]);
        TextView btt = (TextView) convertView.findViewById(mIds[2]);
        EditText bttt = (EditText) convertView.findViewById(mIds[3]);
        TextView a = (TextView) convertView.findViewById(mIds[4]);
        TextView av= (TextView) convertView.findViewById(mIds[5]);
        TextView avt= (TextView) convertView.findViewById(mIds[6]);
        CheckBox check = (CheckBox) convertView.findViewById(mIds[7]);


        if (tt != null) {
              tt.setText(mContent.get(position));
              tt.setTextColor(Color.RED);
       }
        if(bt != null){
              bt.setText("Name: ");
              bt.setTextColor(Color.BLUE);
        }
        if(btt != null){
              btt.setText("Grade:");
              btt.setTextColor(Color.BLUE);
        }
        if(a != null){
          a.setText("Comments:"); 
          a.setTextColor(Color.BLUE);
      }
      if(av != null){
          av.setText("..."); 
          av.setTextColor(Color.RED);
      }
      if(avt != null){
          avt.setText("Present:"); 
          avt.setTextColor(Color.BLUE);
      }

      holder = new ViewHolder(tt);

          ((CheckBox)convertView.findViewById(mIds[7])).setOnCheckedChangeListener(new
              onCheckedChangeListener() { 

            public void onCheckedChanged(CompoundButton buttonView, 
                                                    boolean isChecked) { 
              // TODO Auto-generated method stub 
              if (buttonView.isChecked()) { 
                  checkModel.get(position).status=true;
              } 

            }
           });


    }else {

    holder = (ViewHolder) convertView.getTag();

     }   



       ((CheckBox)convertView.findViewById(mIds[7])).setChecked(checkModel.get(position).status);
     ((EditText) convertView.findViewById(mIds[3])).setText("lalla");

         return convertView;
     }

【问题讨论】:

    标签: android xml


    【解决方案1】:

    要在 textview 旁边保存图像,请在 onCreate() 中的代码中执行以下操作:

              phoneno.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(android.R.drawable.sym_action_call), null);
    

    【讨论】:

      【解决方案2】:

      我认为,android 将点击视为点击ListView 而不是您的TextView。尝试为EditText以外的组件调整这些设置

      android:focusable="false"
      android:focusableInTouchMode="false"
      

      或者试试

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

      为您的EditText

      为什么你有这么多LinearLayouts,试着用RelativeLayout替换它

      【讨论】:

      • 好吧,这似乎有效,但是当我触摸editText时,我可以按预期写入,但是当我在软键盘上按下完成时,它会删除它。
      • 当您在软键盘可见时按下后退按钮会发生什么?我强烈建议你清理你的 xml,因为它对于 list_item 来说似乎真的很重。
      • 我知道是对的?但这是我被要求的。我必须这样做而不是 dataGrid 视图。所以我别无选择。当我按下返回时,我返回一个活动
      • 我的问题是,当我按下 done 时,我已经从 editText 中清楚地看到了
      • 软键盘上的“完成”按钮?还是有自定义按钮?
      猜你喜欢
      • 2016-08-27
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 2018-02-08
      • 2020-08-28
      • 2020-02-13
      • 2017-02-20
      • 1970-01-01
      相关资源
      最近更新 更多