<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/grey"> <ListViewandroid:id="@+id/comment_list"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"android:layout_marginBottom="50dp" /> <LinearLayoutandroid:id="@+id/rl_enroll"android:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"android:layout_alignParentBottom="true"android:background="@color/white"><ImageView android:id="@+id/comment" android:layout_width="32dp" android:layout_height="32dp" android:src="@drawable/comment" android:layout_weight="1" android:layout_gravity="center" /><ImageView android:id="@+id/chat" android:layout_width="23dp" android:layout_height="23dp" android:src="@drawable/chat" android:layout_weight="1" android:layout_gravity="center"/> </LinearLayout> <RelativeLayoutandroid:id="@+id/rl_comment"android:layout_width="match_parent"android:layout_height="50dp"android:background="@color/white"android:visibility="gone"android:layout_alignParentBottom="true"><View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/grey" /><TextView android:id="@+id/hide_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hide_down" android:textSize="13sp" android:textColor="@color/txtgrey" android:drawableBottom="@drawable/hide_dowm" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp"/><View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/grey" android:layout_toRightOf="@id/hide_down" android:layout_marginLeft="10dp"/><EditText android:id="@+id/comment_content" android:hint="@string/comment_content" android:textSize="15sp" android:singleLine="true" android:layout_width="240dp" android:layout_height="match_parent" android:background="@null" android:layout_toRightOf="@id/hide_down" android:layout_marginLeft="20dp"/><Button android:id="@+id/comment_send" android:layout_width="50dp" android:layout_height="35dp" android:layout_margin="5dp" android:text="@string/send" android:textSize="13sp" android:textColor="@color/white" android:background="@color/mainColor" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_marginLeft="15dp"/> </RelativeLayout></RelativeLayout>2、创建评论内容实体类、 内容适配器、内容的Item布局
public class Comment { String name; //评论者 String content; //评论内容 public Comment(){ } public Comment(String name, String content){this.name = name;this.content = content; } public String getName() {return name; } public void setName(String name) {this.name = name; } public String getContent() {return content; } public void setContent(String content) {this.content = content; }}2)内容适配器 AdapterComment
public class AdapterComment extends BaseAdapter { Context context; List<Comment> data; public AdapterComment(Context c, List<Comment> data){this.context = c;this.data = data; } @Override public int getCount() {return data.size(); } @Override public Object getItem(int i) {return data.get(i); } @Override public long getItemId(int i) {return i; } @Override public View getView(int i, View convertView, ViewGroup viewGroup) {ViewHolder holder;// 重用convertViewif(convertView == null){ holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.item_comment, null); holder.comment_name = (TextView) convertView.findViewById(R.id.comment_name); holder.comment_content = (TextView) convertView.findViewById(R.id.comment_content); convertView.setTag(holder);}else{ holder = (ViewHolder) convertView.getTag();}// 适配数据holder.comment_name.setText(data.get(i).getName());holder.comment_content.setText(data.get(i).getContent());return convertView; } /*** 添加一条评论,刷新列表* @param comment*/ public void addComment(Comment comment){data.add(comment);notifyDataSetChanged(); } /*** 静态类,便于GC回收*/ public static class ViewHolder{TextView comment_name;TextView comment_content; }}3)内容的Item布局 item_comment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextViewandroid:id="@+id/comment_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/mainColor"android:textSize="15sp"android:layout_marginLeft="15dp"android:layout_marginRight="3dp"/> <TextViewandroid:id="@+id/comment_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/colorAccent"android:textSize="15sp" /></LinearLayout>
接下来再完善,具体的实现我已经在代码中做了注释,就不具体说了
public class MainActivity extends Activity implements View.OnClickListener { private ImageView comment; private TextView hide_down; private EditText comment_content; private Button comment_send; private LinearLayout rl_enroll; private RelativeLayout rl_comment; private ListView comment_list; private AdapterComment adapterComment; private List<Comment> data; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView(); } private void initView() {// 初始化评论列表comment_list = (ListView) findViewById(R.id.comment_list);// 初始化数据data = new ArrayList<>();// 初始化适配器adapterComment = new AdapterComment(getApplicationContext(), data);// 为评论列表设置适配器comment_list.setAdapter(adapterComment);comment = (ImageView) findViewById(R.id.comment);hide_down = (TextView) findViewById(R.id.hide_down);comment_content = (EditText) findViewById(R.id.comment_content);comment_send = (Button) findViewById(R.id.comment_send);rl_enroll = (LinearLayout) findViewById(R.id.rl_enroll);rl_comment = (RelativeLayout) findViewById(R.id.rl_comment);setListener(); } /*** 设置监听*/ public void setListener(){comment.setOnClickListener(this);hide_down.setOnClickListener(this);comment_send.setOnClickListener(this); } @Override public void onClick(View v) {switch (v.getId()) { case R.id.comment:// 弹出输入法InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);// 显示评论框rl_enroll.setVisibility(View.GONE);rl_comment.setVisibility(View.VISIBLE);break; case R.id.hide_down:// 隐藏评论框rl_enroll.setVisibility(View.VISIBLE);rl_comment.setVisibility(View.GONE);// 隐藏输入法,然后暂存当前输入框的内容,方便下次使用InputMethodManager im = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);im.hideSoftInputFromWindow(comment_content.getWindowToken(), 0);break; case R.id.comment_send:sendComment();break; default:break;} } /*** 发送评论*/ public void sendComment(){if(comment_content.getText().toString().equals("")){ Toast.makeText(getApplicationContext(), "评论不能为空!", Toast.LENGTH_SHORT).show();}else{ // 生成评论数据 Comment comment = new Comment(); comment.setName("评论者"+(data.size()+1)+":"); comment.setContent(comment_content.getText().toString()); adapterComment.addComment(comment); // 发送完,清空输入框 comment_content.setText(""); Toast.makeText(getApplicationContext(), "评论成功!", Toast.LENGTH_SHORT).show();} }}注意:
当出现这个问题时,可以在Manifest.xml文件中,给对应的Activity添加一条属性
android:windowSoftInputMode="stateHidden|adjustResize"
这样,输入法就可以自动调节,显示画红圈的部分,底部输入框和输入法就不会重叠了。
4、最后的效果图如下
隐藏输入框的界面
显示输入框的界面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。