【问题标题】:Change edittext hint dynamically in toolbar on swipe tabs在滑动选项卡上的工具栏中动态更改编辑文本提示
【发布时间】:2015-11-22 04:34:11
【问题描述】:

这里是工具栏的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- Editext for Search -->
    <EditText android:id="@+id/inputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:drawableLeft="@drawable/ic_search_white"
        android:inputType="textVisiblePassword"/>


</LinearLayout>


</android.support.v7.widget.Toolbar>

这里是主要布局

<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"
tools:context=".MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    />

<com.example.ic071034.materialstabs.SlidingTabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/ColorPrimary"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="1">
</android.support.v4.view.ViewPager>


</LinearLayout>

这是标签片段(可滑动视图)

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class Tab1 extends Fragment {

private ListView mListView;
private ListviewContactAdapter adapter;
private ArrayList<ListviewContactItem> listContact;
private TextView searchbox;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1, container, false);


    mListView = (ListView) rootView.findViewById(R.id.list);
    listContact = GetlistContact();
    adapter=new ListviewContactAdapter(getActivity(), listContact);
    mListView.setAdapter(adapter);
    return rootView;
}
}

现在我想在工具栏中的 edittext 中设置提示文本,当我滑动选项卡时,当我滑动到 tab1 时,我希望提示为“xyz”,当我滑动到 tab2 时,它应该显示“wxy”

主要布局代码

package com.example.ic071034.materialstabs;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;   
import android.support.v7.widget.Toolbar;


public class MainActivity extends ActionBarActivity {

// Declaring Your View and Variables

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Locations","Lines"};
String[] Location ={"xyz","xyz","xyzBoard","xyzLayout","xyz","xyz","xyzxyz","xyz"};
String[] Lines ={"S1","S2","S3","S4","S5","S6","S7","S8"};
int Numboftabs =2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);



    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true,     This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);



}


 /*   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
*/
}

【问题讨论】:

  • 能否添加使用主布局的Activity代码
  • @fisher3421 我已添加代码

标签: android tabs android-edittext hint


【解决方案1】:

首先将TextView 中的MainActivity 初始化为onCreate()

final TextView inputSearch = (TextView) findViewById(R.id.inputSearch);

之后为ViewPager添加监听器

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    String tabName = //get tab name by tab position
                    inputSearch.setHint(tabName);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

【讨论】:

  • @user2067614 你能把我的答案标记为正确并投票)
  • 我试过了,但由于我是这个社区的新用户,所以它不会公开显示。对不起。
猜你喜欢
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
相关资源
最近更新 更多