簡體   English   中英

應用程序啟動時彈出菜單

[英]Pop up menu on Application start

我有一個可以在開始時選擇語言的應用程序。 當前,當應用程序啟動時,我必須單擊“選擇語言”,然后彈出包含不同語言的窗口。 但我不想單擊“選擇語言”選項以顯示彈出窗口。 我希望我的應用在啟動時自動顯示彈出窗口以選擇語言。 我遵循了教程。 這是我想要實現的目標的快照。

在此處輸入圖片說明

這是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);
    }
}

我的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>

在您的Activity 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... 

Activity onCreate期間它將自動打開spinner彈出窗口

更新

取消可運行的使用:

宣布:

 Handler handler;
 Runnable runnable;

里面onCreate()

handler = new Handler();
runnable = new Runnable() {
         public void run() {
             spinnerctrl.performClick();  
       }
   };
handler.postDelayed(this, 500);

現在,使用以下命令在onItemSelected取消它:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM