如何在当前选项卡布局中设置点击事件
how to set click event in current tab layout
我有两个选项卡,一个是登录,第二个是查询。我在两个选项卡中都设置了布局。但我不知道如何在登录 activity 和查询 activity 上设置点击事件。
举个例子,目前我在登录选项卡上。在登录时,我有两个按钮提交和取消,如果用户按下提交转到下一页..我是怎么做到的
LoginEnquiryTab.java
public class LoginEnquiryTab extends AppCompatActivity {
TabHost myTabHost;
//LocalActivityManager mlam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
{
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
}
//mlam = new LocalActivityManager(this, false);
//mlam.dispatchCreate(savedInstanceState);
myTabHost =(TabHost) findViewById(android.R.id.tabhost);
myTabHost.setup();
TabHost.TabSpec login = myTabHost.newTabSpec("tab1");
TabHost.TabSpec enquiry = myTabHost.newTabSpec("tab2");
//Below name is display on screen
login.setIndicator(getResources().getString(R.string.btn_login));
login.setContent(R.id.tab1);
enquiry.setIndicator(getResources().getString(R.string.enquiry));
enquiry.setContent(R.id.tab2);
myTabHost.addTab(login);
myTabHost.addTab(enquiry);
myTabHost.setCurrentTab(0);
}
@Override
protected void onResume(){
super.onResume();
//mlam.dispatchResume();
}
@Override
protected void onPause(){
super.onPause();
// mlam.dispatchPause(isFinishing());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<LinearLayout android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/login" />
</LinearLayout>
<LinearLayout android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/changepwd" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
看我做了一个示例程序。但我正在使用 Fragments 作为标签内容-
Class TabActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TabHost;
import android.widget.Toast;
/**
* Created by jimitpatel on 14/04/16.
*/
public class TabActivity extends AppCompatActivity implements OnTabEvent {
private FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
// create the TabHost that will contain the Tabs
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab2.setIndicator("Tab2");
tab3.setIndicator("Tab3");
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1, Tab1Fragment.class, null);
tabHost.addTab(tab2, Tab2Fragment.class, null);
tabHost.addTab(tab3, Tab3Fragment.class, null);
}
@Override
public void onButtonClick(String text) {
if (!TextUtils.isEmpty(text) && null != tabHost) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
switch (text) {
case "Tab1" :
tabHost.setCurrentTab(1);
break;
case "Tab2" :
tabHost.setCurrentTab(2);
break;
case "Tab3" :
tabHost.setCurrentTab(0);
break;
}
}
}
}
是xml个文件activity_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Class Tab1Fragment.java
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Tab1Fragment extends Fragment {
private OnTabEvent mListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
Button button = (Button) view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onButtonClick("Tab1");
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnTabEvent) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
}
}
}
Class Tab2Fragment.java
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Tab2Fragment extends Fragment {
private OnTabEvent mListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab2, container, false);
Button button = (Button) view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onButtonClick("Tab2");
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnTabEvent) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
}
}
}
Tab3Framgent
也类似。
TabOnEvent
这里是在 TabActivity
中实现的接口,它的回调在 Fragment classes
中使用
界面TabOnEvent.java
public interface OnTabEvent {
void onButtonClick(String text);
}
我想这足以满足您的需求。单击按钮时从 Fragment
调用 onButtonClick
方法。界面将确保它在 activity class 中被回调,然后您可以根据需要更改选项卡。
我有两个选项卡,一个是登录,第二个是查询。我在两个选项卡中都设置了布局。但我不知道如何在登录 activity 和查询 activity 上设置点击事件。 举个例子,目前我在登录选项卡上。在登录时,我有两个按钮提交和取消,如果用户按下提交转到下一页..我是怎么做到的
LoginEnquiryTab.java
public class LoginEnquiryTab extends AppCompatActivity {
TabHost myTabHost;
//LocalActivityManager mlam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
{
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
}
//mlam = new LocalActivityManager(this, false);
//mlam.dispatchCreate(savedInstanceState);
myTabHost =(TabHost) findViewById(android.R.id.tabhost);
myTabHost.setup();
TabHost.TabSpec login = myTabHost.newTabSpec("tab1");
TabHost.TabSpec enquiry = myTabHost.newTabSpec("tab2");
//Below name is display on screen
login.setIndicator(getResources().getString(R.string.btn_login));
login.setContent(R.id.tab1);
enquiry.setIndicator(getResources().getString(R.string.enquiry));
enquiry.setContent(R.id.tab2);
myTabHost.addTab(login);
myTabHost.addTab(enquiry);
myTabHost.setCurrentTab(0);
}
@Override
protected void onResume(){
super.onResume();
//mlam.dispatchResume();
}
@Override
protected void onPause(){
super.onPause();
// mlam.dispatchPause(isFinishing());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<LinearLayout android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/login" />
</LinearLayout>
<LinearLayout android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/changepwd" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
看我做了一个示例程序。但我正在使用 Fragments 作为标签内容-
Class TabActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TabHost;
import android.widget.Toast;
/**
* Created by jimitpatel on 14/04/16.
*/
public class TabActivity extends AppCompatActivity implements OnTabEvent {
private FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
// create the TabHost that will contain the Tabs
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Tab1");
tab2.setIndicator("Tab2");
tab3.setIndicator("Tab3");
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1, Tab1Fragment.class, null);
tabHost.addTab(tab2, Tab2Fragment.class, null);
tabHost.addTab(tab3, Tab3Fragment.class, null);
}
@Override
public void onButtonClick(String text) {
if (!TextUtils.isEmpty(text) && null != tabHost) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
switch (text) {
case "Tab1" :
tabHost.setCurrentTab(1);
break;
case "Tab2" :
tabHost.setCurrentTab(2);
break;
case "Tab3" :
tabHost.setCurrentTab(0);
break;
}
}
}
}
是xml个文件activity_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Class Tab1Fragment.java
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Tab1Fragment extends Fragment {
private OnTabEvent mListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
Button button = (Button) view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onButtonClick("Tab1");
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnTabEvent) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
}
}
}
Class Tab2Fragment.java
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Tab2Fragment extends Fragment {
private OnTabEvent mListener;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab2, container, false);
Button button = (Button) view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onButtonClick("Tab2");
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnTabEvent) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnTabEvent interface");
}
}
}
Tab3Framgent
也类似。
TabOnEvent
这里是在 TabActivity
中实现的接口,它的回调在 Fragment classes
界面TabOnEvent.java
public interface OnTabEvent {
void onButtonClick(String text);
}
我想这足以满足您的需求。单击按钮时从 Fragment
调用 onButtonClick
方法。界面将确保它在 activity class 中被回调,然后您可以根据需要更改选项卡。