Android 应用程序启动时的弹出菜单

Android 应用程序启动时的弹出菜单,android,xml,Android,Xml,我有一个应用程序,可以在开始时选择语言。当前,当应用程序启动时,我必须单击“选择语言”,然后出现包含不同语言的弹出窗口。但我不想点击“选择语言”选项来显示弹出窗口。我希望我的应用程序在应用程序启动时自动显示一个选择语言的弹出窗口。我跟着 辅导的这是我想要实现的事情的快照 下面是Mainactivity.java的代码 public class AndroidLocalize extends Activity { Spinner spinnerctrl; Button btn;

我有一个应用程序,可以在开始时选择语言。当前,当应用程序启动时,我必须单击“选择语言”,然后出现包含不同语言的弹出窗口。但我不想点击“选择语言”选项来显示弹出窗口。我希望我的应用程序在应用程序启动时自动显示一个选择语言的弹出窗口。我跟着 辅导的这是我想要实现的事情的快照

下面是Mainactivity.java的代码

public class AndroidLocalize extends Activity {
    Spinner spinnerctrl;
    Button btn;
    Locale myLocale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinnerctrl = (Spinner) findViewById(R.id.spinner1);
        spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (pos == 1) {
                    Toast.makeText(parent.getContext(), "You have selected Tamil", Toast.LENGTH_SHORT).show();
                    setLocale("ta");
                } else if (pos == 2) {
                    Toast.makeText(parent.getContext(), "You have selected Hindi", Toast.LENGTH_SHORT).show();
                    setLocale("hi");
                } else if (pos == 3) {
                    Toast.makeText(parent.getContext(), "You have selected English", Toast.LENGTH_SHORT).show();
                    setLocale("en");
                }
                else if (pos == 4) {
                    Toast.makeText(parent.getContext(), "You have selected Arabic", Toast.LENGTH_SHORT).show();
                    setLocale("ar");
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
    }

    public void setLocale(String lang) {
        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, AndroidLocalize.class);
        startActivity(refresh);
    }
}
public类AndroidLocalize扩展活动{
喷丝器喷丝器;
按钮btn;
本地语言;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
喷丝头Ctrl=(喷丝头)findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(新建OnItemSelectedListener()){
已选择公共位置(AdapterView父项、视图、整数位置、长id){
如果(位置==1){
Toast.makeText(parent.getContext(),“您已选择泰米尔语”,Toast.LENGTH_SHORT.show();
setLocale(“ta”);
}否则如果(位置==2){
Toast.makeText(parent.getContext(),“您已经选择了印地语”,Toast.LENGTH_SHORT.show();
setLocale(“hi”);
}否则如果(位置==3){
Toast.makeText(parent.getContext(),“您选择了英语”,Toast.LENGTH_SHORT.show();
设置语言环境(“en”);
}
否则如果(位置==4){
Toast.makeText(parent.getContext(),“您已经选择了阿拉伯语”,Toast.LENGTH_SHORT.show();
setLocale(“ar”);
}
}
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
}
公共void setLocale(字符串lang){
myLocale=新语言环境(lang);
Resources res=getResources();
DisplayMetrics dm=res.getDisplayMetrics();
Configuration=res.getConfiguration();
conf.locale=myLocale;
res.updateConfiguration(conf,dm);
意图刷新=新意图(这是AndroidLocalize.class);
星触觉(刷新);
}
}
My main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/greet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/greet"
        android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sachin" android:paddingTop="25sp" />

    <TextView
        android:textColor="#ffffff"
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/langselection"
        android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>

    <Spinner
        android:popupBackground="#ff004372"
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/languages"
        android:gravity="center" 
        android:paddingTop="25sp" />
</LinearLayout>

在您的
活动的
onCreate()
中尝试以下操作:

spinnerctrl = (Spinner) findViewById(R.id.spinner1);
findViewById(android.R.id.content).post(new Runnable() {
            @Override
            public void run() {
                 spinnerctrl.performClick();  
            }
});
//Rest of the code... 
一旦创建
活动

更新

要取消可运行使用,请执行以下操作:

宣布:

 Handler handler;
 Runnable runnable;
内部
onCreate()

现在使用以下方法在选中的
中取消它:

 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (pos == 1) {
                Toast.makeText(parent.getContext(), "You have selected Tamil", Toast.LENGTH_SHORT).show();
                setLocale("ta");
                handler.removeCallbacks(runnable); 
            } 
            //rest of the condition
public void已选择(AdapterView父视图、视图视图、整数位置、长id){
如果(位置==1){
Toast.makeText(parent.getContext(),“您已选择泰米尔语”,Toast.LENGTH_SHORT.show();
setLocale(“ta”);
handler.removeCallbacks(可运行);
} 
//其余情况

您可以在上面的代码中编辑它并粘贴到这里吗?只需调用
performClick()
设置适配器后微调器上的选项我没有任何适配器代码。我遵循本教程。logcat上没有错误,应用程序已安装。当我启动它时,它在我的手机上崩溃。你必须将手机与安卓studio连接以查看logcat错误。。。
 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (pos == 1) {
                Toast.makeText(parent.getContext(), "You have selected Tamil", Toast.LENGTH_SHORT).show();
                setLocale("ta");
                handler.removeCallbacks(runnable); 
            } 
            //rest of the condition