2014-06-10 245 views
-1

我正在尝试将ActionBarSherlock导航抽屉中的TextViews更改为带有提示的EditTexts。 到目前为止,我已经完成从它创建编辑文本(这是一块蛋糕),但我似乎无法找到如何创建从文本提示,现在显示为填充editTexts现在..ActionbarSherlock导航抽屉

怎么可以我改变它? 以下是我的代码。 因为我不知道哪个部分是相关的,我发布了很多代码。

mFragmentTitles = getResources().getStringArray(R.array.nav_drawer_items); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
    mDrawerList = (ListView)findViewById(R.id.drawer_list); 
    //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mFragmentTitles)); 
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 

    mDrawerToggle = new ActionBarDrawerToggle(this, 
      mDrawerLayout, 
      R.drawable.ic_drawer, 
      R.string.drawer_open, 
      R.string.drawer_close){ 
     public void onDrawerClosed(View v){ 
      getSupportActionBar().setTitle(mTitle); 
      supportInvalidateOptionsMenu(); 
     } 
     public void onDrawerOpened(View v){ 
      getSupportActionBar().setTitle(mDrawerTitle); 
      supportInvalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 
    if(savedInstanceState == null){ 
     selectItem(0); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getSupportMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
    case android.R.id.home: 
     if(mDrawerLayout.isDrawerOpen(mDrawerList)){ 
      mDrawerLayout.closeDrawer(mDrawerList); 
     }else { 
      mDrawerLayout.openDrawer(mDrawerList); 
     } 
     return true; 
    case R.id.settings: 
     if(mDrawerLayout.isDrawerOpen(mDrawerList)){ 
      mDrawerLayout.closeDrawer(mDrawerList); 
     }else { 
      mDrawerLayout.openDrawer(mDrawerList); 
     } 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 
private class DrawerItemClickListener implements ListView.OnItemClickListener{ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id){ 
     selectItem(position); 
    } 
} 
private void selectItem(int position){ 
    Fragment newFragment = new Fragment_1(); 
    FragmentManager fm = getSupportFragmentManager(); 
    switch(position){ 
    case 0: 
     newFragment = new Fragment_1(); 
     break; 
    case 1: 
     newFragment = new fragment_2(); 
     break; 
    case 2: 
     newFragment = new fragment_3(); 
     break; 
    case 3: 
     newFragment = new fragment_4(); 
     break; 
    case 4: 
     newFragment = new Fragment_1(); 
     break; 
    } 
    fm.beginTransaction() 
    .replace(R.id.content_frame, newFragment) 
    .commit(); 

    mDrawerList.setItemChecked(position, true); 
    setTitle(mFragmentTitles[position]); 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

@Override 
public void setTitle(CharSequence title){ 
    mTitle = title; 
    getSupportActionBar().setTitle(title); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState){ 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig){ 
    super.onConfigurationChanged(newConfig); 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

} 

以我XML是一个EditText(drawer_list_item.xml)

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="16dp" 
android:paddingRight="16dp" 
android:gravity="center_vertical" 
android:textColor="#fff" 
android:minHeight="48dp"/> 

,这是我的其他XML我使用显示抽屉。

<android.support.v4.widget.DrawerLayout  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:id="@+id/drawer" > 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/content_frame"/> 
<ListView 
    android:id="@+id/drawer_list" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:divider="#0c7f58" 
    android:dividerHeight="1dp" 
    android:background="#13ca8c" 
    android:choiceMode="singleChoice" 
    android:layout_gravity="end" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    /> 

</android.support.v4.widget.DrawerLayout> 

like so

+0

请发表你设置的EditText的提示部分。 – matiash

+0

这正是我的问题,我在哪里做。 – user3671459

+0

但EditText在哪里?它是否在布局xml文件中?还是你以编程方式创建它(用Java代码)?因为它不在这些片段:)请张贴该部分。 – matiash

回答

0

解决这个问题的方法是创建一个自定义的MenuAdapter并将编辑文本放在那里。

mMenuAdapter = new MenuListAdapter(MainFragActivity.this, title); 
    mDrawerList.setAdapter(mMenuAdapter); 

//在你的主类放上面的代码,然后在你的自定义类中放下面的代码;


import com.actionbarsherlock.app.SherlockFragment; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MenuListAdapter extends BaseAdapter { 

// Declare variables 
Context context; 
String[] mTitle; 
String[] mSubTitle; 
LayoutInflater inflater; 

public MenuListAdapter(Context context, String[] title) { 
    this.context = context; 
    this.mTitle = title; 
} 

@Override 
public int getCount() { 
    return mTitle.length; 
} 

@Override 
public Object getItem(int position) { 
    return mTitle[position]; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Declare Variables 
    EditText txtTitle; 
    TextView txtSubTitle; 
    ImageView imgIcon; 

    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View itemView = inflater.inflate(R.layout.drawer_list_item, parent, false); 

    // Locate the TextViews 
    txtTitle = (EditText)itemView.findViewById(R.id.title); 
    // Set the data 
    txtTitle.setHint(mTitle[position]); 
    txtTitle.setBackground(null); 

    return itemView; 

} 

}

0

一种ArrayAdapter,默认情况下,装载每一个项目的字符串值作为相应的TextView的文本。这是落实getView()是:

if (convertView == null) { 
     view = mInflater.inflate(resource, parent, false); 
    } else { 
     view = convertView; 
    } 

    <irrelevant stuff snipped> 

    T item = getItem(position); 
    if (item instanceof CharSequence) { 
     text.setText((CharSequence)item); 
    } else { 
     text.setText(item.toString()); 
    } 

如果你想要做的是加载的提示,而不是文字的,你应该重写此方法。例如,如下:

EditText edit = (EditText)LayoutInflater.from(getContext()).inflate(R.layout.drawer_list_item); 
edit.setHint(getItem(position).toString()); 

这将得到相应的字符串作为提示,而不是文本。

+0

是的,我知道,但它不是一个普通的EditText,它是在一个导航抽屉(如果我再次打开它的字符串数组tekst显示) – user3671459

+0

我不明白。一个'EditText'在任何地方都是一样的,即使在导航抽屉里。 – matiash

+0

我从值/ strings.xml中的字符串数组中获取文本,这是通过ArrayAdapter添加到列表视图中的。然而,如果我打开列表视图编辑文本充满字符串数组作为tekst,所以不作为我想要它们的提示。 – user3671459