首页 新闻 会员 周边 捐助

Android中ListView的item为EditText,点击EditText出现键盘但马上键盘就退出了

0
悬赏园豆:5 [待解决问题]

代码可以在以下网址下载:

http://vdisk.weibo.com/s/FDYvnsW1WJLol

1.MainActivity.java

 1 package com.example.mytest1;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.os.Bundle;
 7 import android.support.v7.app.ActionBarActivity;
 8 import android.widget.ListView;
 9 
10 public class MainActivity extends ActionBarActivity {
11     private ListView freegift_lv;
12     private FreeGiftListAdapter adapter_gift;
13     private List<String> stringList;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_freegift);
18         stringList = new ArrayList<String>();
19         stringList.add(new String("String 1"));
20         stringList.add(new String("String 2"));
21         stringList.add(new String("String 3"));
22         stringList.add(new String("String 4"));
23         freegift_lv = (ListView) findViewById(R.id.freegift_lv);
24         showList_gift(stringList);
25         /*but = (Button) findViewById(R.id.freegift_lv);
26         but.setOnClickListener(new OnClickListener() {
27             
28             @Override
29             public void onClick(View arg0) {
30                 Dialog builder = new Dialog(MainActivity.this, R.style.MyDialog);
31                 Window dialogWindow = builder.getWindow();
32                 WindowManager m = getWindowManager();
33                 Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
34                 WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
35                 p.height = (int) (d.getHeight() * 1.0); // 高度设置为屏幕的0.6
36                 p.width = (int) (d.getWidth() * 0.65); // 宽度设置为屏幕的0.65
37                 dialogWindow.setAttributes(p);
38                 Window dialogWindow = builder.getWindow();
39                 WindowManager.LayoutParams lp = dialogWindow.getAttributes();
40                 dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
41                 lp.x = 100; // 新位置X坐标
42                 lp.y = 100; // 新位置Y坐标
43                 lp.width = 300; // 宽度
44                 lp.height = 300; // 高度
45                 lp.alpha = 0.7f; // 透明度
46                 dialogWindow.setAttributes(lp);
47                 View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_style, null);
48                 builder.setCancelable(true);
49                 builder.setContentView(view);
50                 text1 = (TextView) findViewById(R.id.dialog_title);
51                 text1 = (TextView) findViewById(R.id.dialog_content);
52                 but1 = (Button) findViewById(R.id.dialog_confirm);
53                 builder.show();
54             }
55         });*/
56     }
57     private void showList_gift(List<String> entityList) {
58         if (adapter_gift == null) {
59             adapter_gift = new FreeGiftListAdapter(entityList, this);
60             freegift_lv.setAdapter(adapter_gift);
61         } else {
62             adapter_gift.changeData(entityList);
63         }
64 
65     }
66 }

2.FreeGiftListAdapter.java

 1 package com.example.mytest1;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.content.Context;
 7 import android.graphics.BitmapFactory;
 8 import android.util.Log;
 9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.view.View.OnFocusChangeListener;
12 import android.view.ViewGroup;
13 import android.widget.BaseAdapter;
14 import android.widget.EditText;
15 import android.widget.ImageView;
16 import android.widget.LinearLayout;
17 import android.widget.TextView;
18 
19 
20 public class FreeGiftListAdapter extends BaseAdapter {
21     private List<String> dataSource;
22     private LayoutInflater inflater;
23     private ViewHolder holder;
24 //    private Context context;
25     
26     public FreeGiftListAdapter(List<String> dataSource, Context context) {
27 //        this.context=context;
28         this.dataSource = dataSource;
29         this.inflater = LayoutInflater.from(context);
30     }
31     
32     public void changeData(List<String> giftList) {
33         this.notifyDataSetChanged();
34     }
35     
36     @Override
37     public int getCount() {
38         return dataSource.size();
39     }
40 
41     @Override
42     public Object getItem(int arg0) {
43         return dataSource.get(arg0);
44     }
45 
46     @Override
47     public long getItemId(int arg0) {
48         return arg0;
49     }
50 
51     @Override
52     public View getView(int position, View convertView, ViewGroup parent) {
53         if (convertView == null) {
54             holder = new ViewHolder();
55             convertView = inflater.inflate(R.layout.activity_freegift_item, null);
56             holder.gift_num = (EditText) convertView
57                     .findViewById(R.id.gift_num);
58             holder.name = (TextView) convertView.findViewById(R.id.name);
59             convertView.setTag(holder);
60         } else {
61             holder = (ViewHolder) convertView.getTag();
62         }
63         holder.name.setText(dataSource.get(position));
64         holder.gift_num.setOnFocusChangeListener(new OnFocusChangeListenerImpl(position));
65         return convertView;
66     }
67 
68     class ViewHolder {
69         EditText gift_num;
70         TextView name;
71     }
72     private class OnFocusChangeListenerImpl implements OnFocusChangeListener {
73         private int position;
74         public OnFocusChangeListenerImpl(int position) {
75             this.position = position;
76         }
77         @Override
78         public void onFocusChange(View arg0, boolean arg1) {
79             EditText et = (EditText) arg0;
80             if(arg1) {
81                 Log.e("", "获得焦点");
82             } else {
83                 Log.e("", "失去焦点");
84             }
85         }
86         
87     }
88 }

3.MainActivity.java的布局文件activity_freegift.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" 
 6   >
 7   <ListView
 8       android:id="@+id/freegift_lv"
 9       android:layout_width="match_parent"
10       android:layout_height="match_parent" />
11 
12 </LinearLayout>

4.adapter的布局activity_freegift_item.xml

<LinearLayout 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"
    android:orientation="vertical" 
  >
  <ListView
      android:id="@+id/freegift_lv"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>
5.res/drawable/dialog_title_background.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 3 
 4     <item><shape>
 5             <solid android:color="#888900" />
 6 
 7             <stroke android:width="1.0dip" android:color="#ffcccccc" />
 8 
 9             <corners android:topRightRadius="6dp" android:topLeftRadius="6dp" />
10         </shape></item>
11 
12 </selector>

6.res/drawable/dialog_background.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 3 
 4     <item><shape>
 5             <padding android:bottom="4dip" android:left="4dip" android:right="4dip" android:top="4dip" />
 6             <solid android:color="#FF00FF" />
 7 
 8             <stroke android:width="1.0dip" android:color="#ffcccccc" />
 9 
10             <corners android:radius="6dp"/>
11         </shape></item>
12 
13 </selector>

7.res/layout/dialog_style.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <RelativeLayout
 8         android:layout_width="match_parent"
 9         android:gravity="center"
10         android:background="@drawable/dialog_title_background"
11         android:layout_height="wrap_content" >
12 
13         <TextView
14             android:layout_alignParentLeft="true"
15             android:id="@+id/dialog_title"
16             android:text="@string/dialog_title_fgla"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content" />
19         <TextView
20             android:layout_alignParentRight="true"
21             android:id="@+id/dialog_close"
22             android:text="@string/dialog_title_fgla_close"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content" />
25     </RelativeLayout>
26     
27     <LinearLayout
28         android:layout_marginTop="8dp"
29         android:layout_marginBottom="8dp"
30         android:gravity="center_horizontal"
31         android:orientation="vertical"
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content" >
34         <TextView
35             android:text="@string/dialog_msg_fgla_1"
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content" />
38         <TextView
39             android:text="@string/dialog_msg_fgla_2"
40             android:id="@+id/dialog_content"
41             android:layout_width="wrap_content"
42             android:layout_height="wrap_content" />
43     </LinearLayout>
44     
45     <LinearLayout
46         android:layout_marginTop="8dp"
47         android:layout_marginBottom="8dp"
48         android:layout_width="match_parent"
49         android:layout_height="wrap_content"
50         android:gravity="center"
51         android:orientation="horizontal" >
52 
53         <Button
54             android:background="@drawable/dialog_background"
55             android:id="@+id/dialog_confirm"
56             android:text="@string/dialog_submit_fgla"
57             android:layout_width="wrap_content"
58             android:layout_height="30dp" />
59     </LinearLayout>
60 
61 </LinearLayout>

8.res/values/string.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="app_name">MyTest1</string>
 5     <string name="hello_world">Hello world!</string>
 6     <string name="action_settings">Settings</string>
 7     <!-- FreeGiftListAdapter对话框 -->
 8     <string name="dialog_title_fgla">温馨提示</string>
 9     <string name="dialog_title_fgla_close">关闭</string>
10     <string name="dialog_msg_fgla_1">很抱歉,您还没登录或者超时,请先登录。</string>
11     <string name="dialog_msg_fgla_2">请先登录 | 注册</string>
12     <string name="dialog_submit_fgla">确认</string>
13 </resources>

9.res/values/styles.xml

 1 <resources>
 2 
 3     <!--
 4         Base application theme, dependent on API level. This theme is replaced
 5         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
 6     -->
 7     <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
 8         <!--
 9             Theme customizations available in newer API levels can go in
10             res/values-vXX/styles.xml, while customizations related to
11             backward-compatibility can go here.
12         -->
13     </style>
14 
15     <!-- Application theme. -->
16     <style name="AppTheme" parent="AppBaseTheme">
17         <!-- All customizations that are NOT specific to a particular API-level can go here. -->
18     </style>
19     <style name="MyDialog" parent="@android:Theme.Dialog">
20         <item name="android:windowFrame">@null</item>
21         <item name="android:windowNoTitle">true</item>
22         <item name="android:windowBackground">@drawable/dialog_background</item>
23         <item name="android:windowIsFloating">true</item>
24         <item name="android:windowContentOverlay">@null</item>
25     </style>
26 </resources>

 

hanrong的主页 hanrong | 初学一级 | 园豆:166
提问于:2014-10-23 17:04
< >
分享
所有回答(1)
0

代码太长啦,没看完。键盘退出的问题可以设置为:当EditText获取焦点时,重新打开键盘。如下代码

((InputMethodManager) myActivity.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput()

Set sail | 园豆:540 (小虾三级) | 2014-10-24 12:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册