有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java有办法改变安卓。R.布局。简单列表项目1,到我的自定义布局?

部分守则

        package com.example.pass;

 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;

import 安卓.app.Activity;
import 安卓.app.DatePickerDialog;
import 安卓.app.DatePickerDialog.OnDateSetListener;
import 安卓.app.Dialog;
import 安卓.app.TimePickerDialog;
import 安卓.app.TimePickerDialog.OnTimeSetListener;
import 安卓.content.Context;
import 安卓.database.Cursor;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.os.Bundle;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.text.format.DateFormat;
import 安卓.util.Log;
import 安卓.view.KeyEvent;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.view.View.OnKeyListener;
import 安卓.view.ViewGroup;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemLongClickListener;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.CursorAdapter;
import 安卓.widget.DatePicker;
import 安卓.widget.EditText;
import 安卓.widget.ListView;
import 安卓.widget.TextView;
import 安卓.widget.TimePicker;
import 安卓.widget.Toast;




    public class Permission extends ActionBarActivity implements OnClickListener, OnKeyListener{
 EditText Date;
 EditText Time;
 EditText userName;
 EditText userSurname;
 Button btnOK;
 Button Cancel;
 EditText Name;
 String Surname;
 Button clear;
 ListView list;
 ArrayList<String> Items;
 ArrayAdapter<String> aa;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_permission);
    list = (ListView)findViewById(R.id.listView1);


    Items = new ArrayList<String>();
    aa = new ArrayAdapter<String>(this, 安卓.R.layout.simple_list_item_1,Items);
    list.setAdapter(aa);
       // Include dialog.xml file

     // Set dialog title
    final Dialog dialog = new Dialog(Permission.this);
    dialog.setContentView(R.layout.dialog_permission);
    dialog.setTitle("Custom Dialog");

    clear = (Button)findViewById(R.id.clear);
    Button add = (Button) findViewById(R.id.add);



    // Create custom dialog object          
    // set values for custom dialog components - text, image and button
    userName = (EditText) dialog.findViewById(R.id.dialog_username);
    userSurname = (EditText)dialog.findViewById(R.id.dialog_usersurname);
    Time = (EditText)dialog.findViewById(R.id.dialog_time2);
    Date = (EditText)dialog.findViewById(R.id.dialog_date2);    
    btnOK = (Button) dialog.findViewById(R.id.OK);
    Cancel = (Button) dialog.findViewById(R.id.Cancel);
        btnOK.setOnClickListener(this);
        userName.setOnKeyListener(this);


    add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {


           Date.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //To show current date in the datepicker
                    Calendar mcurrentDate=Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog mDatePicker=new DatePickerDialog(Permission.this, new OnDateSetListener() {                  
                        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                            // TODO Auto-generated method stub                      
                            /*      Your code   to get date and time    */
                            Date.setText(selectedyear+"-"+(selectedmonth+1)+"-"+selectedday);
                        }
                    },mYear, mMonth, mDay);
                    mDatePicker.setTitle("Select date");                
                    mDatePicker.show();  }
            });


           Time.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker

                   Calendar mcurrentTime=Calendar.getInstance();
                   int mHour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                   int mMinute = mcurrentTime.get(Calendar.MINUTE);


                   TimePickerDialog mTimePicker=new TimePickerDialog(Permission.this, new OnTimeSetListener() {                  
                       public void onTimeSet(TimePicker timepicker, int selectedhour, int selectedminute ) {
                           // TODO Auto-generated method stub                      
                           /*      Your code   to get date and time    */
                          if (selectedminute<10){
                              Time.setText(selectedhour+":"+"0"+(selectedminute)+""+"val");
                          }
                          else
                        Time.setText(selectedhour+":"+(selectedminute));
                       }
                   },mHour, mMinute, false);
                   mTimePicker.setTitle("Select date");                
                   mTimePicker.show();  }
           });



           Cancel.setOnClickListener(new OnClickListener() {

               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker
                   dialog.dismiss();
                }
           });


        //  OK.setOnClickListener(new OnClickListener() {

            /*   @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                this.addItem(this.userName.getText().toString());
                   //CONVERTING THE TEXT IN TO STRING...


                   System.out.println(Name);
                }
           });*/


           clear.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

                }
           });

           dialog.show();
        }


});

}       



private void addItem(String item){
    if(item.length()>0){
        this.Items.add(item);
        this.aa.notifyDataSetChanged();
        this.userName.setText("");
    }
}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.permission, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}





@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction()==KeyEvent.ACTION_DOWN&&keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
        this.addItem(this.userName.getText().toString());
        this.addItem(this.userSurname.getText().toString());
    }
    return false;
}





@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == this.btnOK){
        this.addItem(this.userName.getText().toString());
        this.addItem(this.userSurname.getText().toString());
        }
    }

   }

每次我尝试更改为自定义布局时,所有内容都会崩溃,我不知道如何才能不丢失对话框窗口,因为我正在读取的文本位于自定义对话框中。我知道也许创建新的适配器类可以解决这个问题,但我想尝试另一种方法


共 (1) 个答案

  1. # 1 楼答案

    使用CTRL+左键单击android.R.layout.simple_list_item_1将其打开。
    将内容复制到您自己的XML布局文件中,并根据需要进行编辑。
    改变

    android.R.layout.simple_list_item_1
    

    R.layout.your_layout