Java 选择Edittext并打开键盘后,自定义Listview才会显示
所以我遇到了一个非常奇怪的问题,我有一个活动,它在一个带有自定义ListView的ListView中显示用户的帖子和日期,从服务器获取并在创建时填充它们 这是活动界面的外观 因此,每当我开始活动时,都会选择edittext部分,并且通常会出现键盘 我用谷歌搜索了如何在启动时不聚焦编辑文本 在父布局中添加这两行可以防止EditText上的焦点Java 选择Edittext并打开键盘后,自定义Listview才会显示,java,android,android-layout,listview,android-edittext,Java,Android,Android Layout,Listview,Android Edittext,所以我遇到了一个非常奇怪的问题,我有一个活动,它在一个带有自定义ListView的ListView中显示用户的帖子和日期,从服务器获取并在创建时填充它们 这是活动界面的外观 因此,每当我开始活动时,都会选择edittext部分,并且通常会出现键盘 我用谷歌搜索了如何在启动时不聚焦编辑文本 在父布局中添加这两行可以防止EditText上的焦点 android:focusable="true" android:focusableInTouchMode="true" 所以我在活动的Relative
android:focusable="true"
android:focusableInTouchMode="true"
所以我在活动的RelativeLayoutXML中添加了这是上图中的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/verticalLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".UserHome">
<EditText
android:id="@+id/tweetText"
android:layout_width="match_parent"
android:layout_height="122dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Write Your Tweet"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/userListHome"
android:layout_alignParentEnd="true"
android:onClick="postTweetFunction"
android:text="Post" />
<ListView
android:id="@+id/userListHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/tweetText"
android:focusable="true"
android:focusableInTouchMode="true"/>
</RelativeLayout>
现在,关于问题部分,我需要在上面说,让您了解问题的确切原因是什么,这是一个奇怪的问题。
因此,在选择编辑文本并打开键盘之前,列表视图不会出现
我确信listview正在填充并且自定义listview正在工作
但在我选择编辑文本并打开键盘之前,listview内容不会出现,就像在选择编辑文本之前listview处于休眠状态一样。
以下是示例
在该活动中查看,并且未选择编辑文本,因此即使填充了listview,也不会显示它。
但只要我点击编辑文本,键盘就会弹出,就像唤醒了睡眠列表视图一样
并且listview和适配器不是匿名的,我已经定义了它们
以下是活动代码(如果需要),以供参考
public class UserHome extends AppCompatActivity {
ListView listView;
ArrayList<String> userOwnTweets;
ArrayList<String> userOwnTweetsDate;
ArrayList<String> getUserOwnTweetsObjectId;
EditText tweetText;
CustomArrayAdapter customArrayAdapter;
Typeface custom_font1, custom_font2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_home);
custom_font1 = Typeface.createFromAsset(getAssets(), "fonts/gooddog.otf");
custom_font2 = Typeface.createFromAsset(getAssets(), "fonts/quicksand.otf");
listView = (ListView) findViewById(R.id.userListHome);
tweetText = (EditText) findViewById(R.id.tweetText);
userOwnTweets = new ArrayList<String>();
userOwnTweetsDate = new ArrayList<String>();
getUserOwnTweetsObjectId = new ArrayList<String>();
customArrayAdapter = new CustomArrayAdapter(getApplicationContext());
listView.setAdapter(customArrayAdapter);
// updating the tweets onCreate
updateTweetList();
}
public void updateTweetList(){
// clearing the lists to avoid duplicate entries
userOwnTweets.clear();
userOwnTweetsDate.clear();
getUserOwnTweetsObjectId.clear();
// just backend code to get the tweets of the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Tweet");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null && objects.size() > 0){
for (ParseObject object : objects){
String tempString = object.getString("tweet");
tempString = tempString.substring(0, Math.min(tempString.length(), 20));
tempString+="...";
// adding them to the arraylists
userOwnTweets.add(tempString);
userOwnTweetsDate.add(object.getString("date"));
getUserOwnTweetsObjectId.add(object.getObjectId());
}
}
}
});
// updating the customlistview for datastatechanged
customArrayAdapter.notifyDataSetChanged();
}
@Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
public void postTweetFunction(View view){
// adding a new tweet and refreshing the tweet list from teh server
ParseObject userTwitter = new ParseObject("Tweet");
userTwitter.put("tweet", tweetText.getText().toString());
userTwitter.put("username", ParseUser.getCurrentUser().getUsername());
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
userTwitter.put("date", String.valueOf(dateFormat.format(new Date())));
userTwitter.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Tweet Added", Toast.LENGTH_SHORT).show();
tweetText.setText("");
updateTweetList();
}
}
});
hideKeyBoard();
}
public void hideKeyBoard(){
// hiding the keyboard
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e){
e.printStackTrace();
}
}
// the custom adapter
class CustomArrayAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
// Constructor
public CustomArrayAdapter(Context context) {
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return userOwnTweets.size();
}
@Override
public Object getItem(int position) {
return userOwnTweets.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = layoutInflater.inflate(R.layout.row_layout, null);
TextView userHomeTweet = (TextView) view.findViewById(R.id.userHomeTweet);
TextView userHomeTweetdate = (TextView) view.findViewById(R.id.userHomeTweetDate);
userHomeTweet.setTypeface(custom_font1);
userHomeTweetdate.setTypeface(custom_font2);
try {
userHomeTweet.setText(userOwnTweets.get(i));
userHomeTweetdate.setText(userOwnTweetsDate.get(i));
}catch(Exception e){
e.printStackTrace();
}
// random color for each
int color = Color.LTGRAY;
view.setBackgroundColor(color);
return view;
}
}
}
公共类UserHome扩展了AppCompative活动{
列表视图列表视图;
ArrayList userOwnTweets;
ArrayList userOwnTweetsDate;
ArrayList GetUserOwnTweetsObject;
编辑文本推特文本;
CustomArrayAdapter CustomArrayAdapter;
字体自定义字体1,自定义字体2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u user\u home);
custom_font1=Typeface.createFromAsset(getAssets(),“font/gooddog.otf”);
custom_font2=Typeface.createFromAsset(getAssets(),“font/quicksand.otf”);
listView=(listView)findViewById(R.id.userListHome);
tweetText=(EditText)findViewById(R.id.tweetText);
userOwnTweets=newarraylist();
userOwnTweetsDate=新的ArrayList();
getUserOwnTweetsObjectId=new ArrayList();
customArrayAdapter=新的customArrayAdapter(getApplicationContext());
setAdapter(customArrayAdapter);
//更新tweets onCreate
updateWeetList();
}
public void updateWeetList(){
//清除列表以避免重复条目
userOwnTweets.clear();
userOwnTweetsDate.clear();
getUserOwnTweetsObjectId.clear();
//只需后端代码即可获取用户的推文
ParseQuery=新的ParseQuery(“推特”);
query.whereEqualTo(“用户名”,ParseUser.getCurrentUser().getUsername());
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null&&objects.size()>0){
for(ParseObject对象:对象){
String tempString=object.getString(“tweet”);
tempString=tempString.substring(0,Math.min(tempString.length(),20));
tempString+=“…”;
//将它们添加到ArrayList中
添加(tempString);
userOwnTweetsDate.add(object.getString(“日期”);
getUserOwnTweetsObjectId.add(object.getObjectId());
}
}
}
});
//更新datastatechanged的customlistview
customArrayAdapter.notifyDataSetChanged();
}
@凌驾
public void onBackPressed(){
Intent Intent=新的Intent(getApplicationContext(),MainActivity.class);
星触觉(意向);
完成();
}
公共void postweet功能(视图){
//添加新tweet并从服务器刷新tweet列表
ParseObject userTwitter=新的ParseObject(“Tweet”);
userTwitter.put(“tweet”,tweetText.getText().toString());
userTwitter.put(“用户名”,ParseUser.getCurrentUser().getUsername());
DateFormat DateFormat=新的简化格式(“yyyy/MM/dd HH:MM”);
userTwitter.put(“date”,String.valueOf(dateFormat.format(new date())));
userTwitter.savenbackground(新的SaveCallback(){
@凌驾
公共作废完成(Parsee异常){
如果(e==null){
Toast.makeText(getApplicationContext(),“添加了Tweet”,Toast.LENGTH_SHORT.show();
tweetText.setText(“”);
updateWeetList();
}
}
});
隐藏板();
}
公共无效隐藏板(){
//隐藏键盘
试一试{
InputMethodManager InputMethodManager=(InputMethodManager)getSystemService(输入方法服务);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
捕获(例外e){
e、 printStackTrace();
}
}
//自定义适配器
类CustomArrayAdapter扩展了BaseAdapter{
语境;
LayoutInflater LayoutInflater;
//建造师
公共CustomArrayAdapter(上下文){
this.context=上下文;
layoutInflater=(layoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u servici
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/userHomeTweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="40dp"
/>
<TextView
android:id="@+id/userHomeTweetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/verticalLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".UserHome">
<EditText
android:id="@+id/tweetText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toStartOf="@id/button"
android:ems="10"
android:hint="Write Your Tweet"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/userListHome"
android:layout_alignParentEnd="true"
android:onClick="postTweetFunction"
android:text="Post" />
<ListView
android:id="@+id/userListHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tweetText"
android:layout_alignParentBottom="true" />
</RelativeLayout>
public void updateTweetList(){
// clearing the lists to avoid duplicate entries
userOwnTweets.clear();
userOwnTweetsDate.clear();
getUserOwnTweetsObjectId.clear();
// just backend code to get the tweets of the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Tweet");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null && objects.size() > 0){
for (ParseObject object : objects){
String tempString = object.getString("tweet");
tempString = tempString.substring(0, Math.min(tempString.length(), 20));
tempString+="...";
// adding them to the arraylists
userOwnTweets.add(tempString);
userOwnTweetsDate.add(object.getString("date"));
getUserOwnTweetsObjectId.add(object.getObjectId());
}
// updating the customlistview for datastatechanged
customArrayAdapter.notifyDataSetChanged();
}
}
});
}